This commit is contained in:
2025-10-01 23:59:31 +03:00
parent 2a29571dbf
commit b6c4b9b6bb
28 changed files with 689 additions and 383 deletions

View File

@@ -4,46 +4,102 @@ using LctMonolith.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using Serilog;
namespace LctMonolith.Services;
public class MissionCategoryService : IMissionCategoryService
namespace LctMonolith.Services
{
private readonly IUnitOfWork _uow;
public MissionCategoryService(IUnitOfWork uow) => _uow = uow;
public async Task<MissionCategory?> GetCategoryByIdAsync(Guid categoryId)
public class MissionCategoryService : IMissionCategoryService
{
try { return await _uow.MissionCategories.GetByIdAsync(categoryId); }
catch (Exception ex) { Log.Error(ex, "GetCategoryByIdAsync failed {CategoryId}", categoryId); throw; }
}
private readonly IUnitOfWork _uow;
public async Task<MissionCategory?> GetCategoryByTitleAsync(string title)
{
try { return await _uow.MissionCategories.Query(c => c.Title == title).FirstOrDefaultAsync(); }
catch (Exception ex) { Log.Error(ex, "GetCategoryByTitleAsync failed {Title}", title); throw; }
}
public MissionCategoryService(IUnitOfWork uow)
{
_uow = uow;
}
public async Task<IEnumerable<MissionCategory>> GetAllCategoriesAsync()
{
try { return await _uow.MissionCategories.Query().OrderBy(c => c.Title).ToListAsync(); }
catch (Exception ex) { Log.Error(ex, "GetAllCategoriesAsync failed"); throw; }
}
public async Task<MissionCategory?> GetCategoryByIdAsync(Guid categoryId)
{
try
{
return await _uow.MissionCategories.GetByIdAsync(categoryId);
}
catch (Exception ex)
{
Log.Error(ex, "GetCategoryByIdAsync failed {CategoryId}", categoryId);
throw;
}
}
public async Task<MissionCategory> CreateCategoryAsync(MissionCategory category)
{
try { category.Id = Guid.NewGuid(); await _uow.MissionCategories.AddAsync(category); await _uow.SaveChangesAsync(); return category; }
catch (Exception ex) { Log.Error(ex, "CreateCategoryAsync failed {Title}", category.Title); throw; }
}
public async Task<MissionCategory?> GetCategoryByTitleAsync(string title)
{
try
{
return await _uow.MissionCategories.Query(c => c.Title == title).FirstOrDefaultAsync();
}
catch (Exception ex)
{
Log.Error(ex, "GetCategoryByTitleAsync failed {Title}", title);
throw;
}
}
public async Task<MissionCategory> UpdateCategoryAsync(MissionCategory category)
{
try { _uow.MissionCategories.Update(category); await _uow.SaveChangesAsync(); return category; }
catch (Exception ex) { Log.Error(ex, "UpdateCategoryAsync failed {Id}", category.Id); throw; }
}
public async Task<IEnumerable<MissionCategory>> GetAllCategoriesAsync()
{
try
{
return await _uow.MissionCategories.Query().OrderBy(c => c.Title).ToListAsync();
}
catch (Exception ex)
{
Log.Error(ex, "GetAllCategoriesAsync failed");
throw;
}
}
public async Task<bool> DeleteCategoryAsync(Guid categoryId)
{
try { var c = await _uow.MissionCategories.GetByIdAsync(categoryId); if (c == null) return false; _uow.MissionCategories.Remove(c); await _uow.SaveChangesAsync(); return true; }
catch (Exception ex) { Log.Error(ex, "DeleteCategoryAsync failed {CategoryId}", categoryId); throw; }
public async Task<MissionCategory> CreateCategoryAsync(MissionCategory category)
{
try
{
category.Id = Guid.NewGuid();
await _uow.MissionCategories.AddAsync(category);
await _uow.SaveChangesAsync();
return category;
}
catch (Exception ex)
{
Log.Error(ex, "CreateCategoryAsync failed {Title}", category.Title);
throw;
}
}
public async Task<MissionCategory> UpdateCategoryAsync(MissionCategory category)
{
try
{
_uow.MissionCategories.Update(category);
await _uow.SaveChangesAsync();
return category;
}
catch (Exception ex)
{
Log.Error(ex, "UpdateCategoryAsync failed {Id}", category.Id);
throw;
}
}
public async Task<bool> DeleteCategoryAsync(Guid categoryId)
{
try
{
var c = await _uow.MissionCategories.GetByIdAsync(categoryId);
if (c == null) { return false; }
_uow.MissionCategories.Remove(c);
await _uow.SaveChangesAsync();
return true;
}
catch (Exception ex)
{
Log.Error(ex, "DeleteCategoryAsync failed {CategoryId}", categoryId);
throw;
}
}
}
}