|
using Backend_Teamwork.src.Database; |
|
using Backend_Teamwork.src.Entities; |
|
using Backend_Teamwork.src.Utils; |
|
using Microsoft.EntityFrameworkCore; |
|
|
|
namespace Backend_Teamwork.src.Repository |
|
{ |
|
public class WorkshopRepository |
|
{ |
|
private readonly DbSet<Workshop> _workshops; |
|
private readonly DatabaseContext _databaseContext; |
|
|
|
public WorkshopRepository(DatabaseContext databaseContext) |
|
{ |
|
_databaseContext = databaseContext; |
|
_workshops = databaseContext.Set<Workshop>(); |
|
} |
|
|
|
|
|
public async Task<Workshop?> CreateOneAsync(Workshop newWorkshop) |
|
{ |
|
await _workshops.AddAsync(newWorkshop); |
|
await _databaseContext.SaveChangesAsync(); |
|
return await GetByIdAsync(newWorkshop.Id); |
|
} |
|
|
|
|
|
public async Task<Workshop?> GetByIdAsync(Guid id) |
|
{ |
|
return await _workshops.Include(o => o.User).FirstOrDefaultAsync(o => o.Id == id); |
|
} |
|
|
|
|
|
public async Task<bool> DeleteOneAsync(Workshop deleteWorkshop) |
|
{ |
|
_workshops.Remove(deleteWorkshop); |
|
await _databaseContext.SaveChangesAsync(); |
|
return true; |
|
} |
|
|
|
|
|
public async Task<bool> UpdateOneAsync(Workshop updateWorkshop) |
|
{ |
|
if (updateWorkshop == null) |
|
return false; |
|
_workshops.Update(updateWorkshop); |
|
await _databaseContext.SaveChangesAsync(); |
|
return true; |
|
} |
|
|
|
|
|
public async Task<List<Workshop>> GetAllAsync() |
|
{ |
|
return await _workshops.Include(o => o.User).ToListAsync(); |
|
} |
|
|
|
public async Task<List<Workshop>> GetAllAsync(PaginationOptions paginationOptions) |
|
{ |
|
|
|
var userQuery = _workshops.Where(a => |
|
a.Name.ToLower().Contains(paginationOptions.Search.ToLower()) |
|
|| a.Location.ToLower().Contains(paginationOptions.Search.ToLower()) |
|
); |
|
|
|
|
|
userQuery = userQuery |
|
.Skip((paginationOptions.PageNumber - 1) * paginationOptions.PageSize) |
|
.Take(paginationOptions.PageSize); |
|
|
|
|
|
userQuery = paginationOptions.SortOrder switch |
|
{ |
|
"name_desc" => userQuery.OrderByDescending(a => a.Name), |
|
"location_asc" => userQuery.OrderBy(a => a.Location), |
|
"location_desc" => userQuery.OrderByDescending(a => a.Location), |
|
"price_desc" => userQuery.OrderByDescending(a => a.Price), |
|
"price_asc" => userQuery.OrderBy(a => a.Price), |
|
"date_desc" => userQuery.OrderByDescending(a => a.CreatedAt), |
|
"date_asc" => userQuery.OrderBy(a => a.CreatedAt), |
|
"capacity_desc" => userQuery.OrderByDescending(a => a.Capacity), |
|
_ => userQuery.OrderBy(a => a.Name), |
|
}; |
|
|
|
return await userQuery.ToListAsync(); |
|
} |
|
} |
|
} |
|
|