-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
802c5c1
commit 2675fcb
Showing
10 changed files
with
200 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using MixyBoos.Api.Data.Models; | ||
|
||
namespace MixyBoos.Api.Data.Repositories; | ||
|
||
public class MixRepository : Repository<Mix> { | ||
public MixRepository(MixyBoosContext context) : base(context) { } | ||
|
||
private IQueryable<Mix> _internalGet(Expression<Func<Mix, bool>> predicate) { | ||
return entities | ||
.Where(predicate) | ||
.Where(m => m.IsProcessed) | ||
.Include(m => m.User) | ||
.Include(m => m.Likes) | ||
.Include(m => m.Plays) | ||
.Include(m => m.Shares) | ||
.Include(m => m.Downloads); | ||
} | ||
|
||
public async Task<IEnumerable<Mix>> GetByUser(string userSlug) { | ||
return await _internalGet(m => m.User.Slug.Equals(userSlug)) | ||
.Where(m => m.IsProcessed) | ||
.ToListAsync(); | ||
} | ||
|
||
public async Task<Mix> GetByUserAndSlug(string userSlug, string mixSlug) { | ||
return await _internalGet(m => m.User.Slug.Equals(userSlug) && m.Slug.Equals(mixSlug)) | ||
.FirstOrDefaultAsync(); | ||
} | ||
|
||
public async Task<IEnumerable<Mix>> GetFeedForUser(Guid id) { | ||
return await _internalGet(m => m.User.Id.Equals(id)) | ||
.Where(m => m.IsProcessed) | ||
.OrderByDescending(m => m.DateCreated) | ||
.ToListAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using MixyBoos.Api.Data.Models; | ||
using MixyBoos.Api.Services.Extensions; | ||
|
||
namespace MixyBoos.Api.Data.Repositories; | ||
|
||
public interface IRepository<T> where T : BaseEntity { | ||
IQueryable<T> GetAll(); | ||
|
||
Task<T> Get(Guid id); | ||
|
||
Task Insert(T entity); | ||
|
||
Task Update(T entity); | ||
|
||
Task Delete(T entity); | ||
} | ||
|
||
public class Repository<T> : IRepository<T> where T : BaseEntity { | ||
private readonly MixyBoosContext context; | ||
protected DbSet<T> entities; | ||
string errorMessage = string.Empty; | ||
|
||
public Repository(MixyBoosContext context) { | ||
this.context = context; | ||
entities = context.Set<T>(); | ||
} | ||
|
||
public IQueryable<T> GetAll() { | ||
return entities.AsQueryable<T>(); | ||
} | ||
|
||
public async Task<T> Get(Guid id) { | ||
return await entities.SingleOrDefaultAsync(s => s.Id.Equals(id)); | ||
} | ||
|
||
public async Task Insert(T entity) { | ||
ArgumentNullException.ThrowIfNull(entity); | ||
|
||
entities.Add(entity); | ||
await context.SaveChangesAsync(); | ||
} | ||
|
||
public async Task Update(T entity) { | ||
ArgumentNullException.ThrowIfNull(entity); | ||
|
||
await context.SaveChangesAsync(); | ||
} | ||
|
||
public async Task AddOrUpdate(T entity) { | ||
ArgumentNullException.ThrowIfNull(entity); | ||
await context.AddOrUpdate(entity); | ||
await context.SaveChangesAsync(); | ||
} | ||
|
||
public async Task Delete(T entity) { | ||
ArgumentNullException.ThrowIfNull(entity); | ||
|
||
entities.Remove(entity); | ||
await context.SaveChangesAsync(); | ||
} | ||
} |
Oops, something went wrong.