From 78fe255aaeebade2ec8547d598e8611d5a8e1e57 Mon Sep 17 00:00:00 2001 From: Muhammadziyo Mahkamov <127393000+Ziyobek617@users.noreply.github.com> Date: Mon, 5 Feb 2024 22:52:31 +0500 Subject: [PATCH] Create EventCategories DTOs, Repositories, Services are done. --- EventBor.Backend.API/appsettings.json | 2 +- .../EventCategoryForCreationDto.cs | 7 ++ .../EventCategoryForResultDto.cs | 11 +++ .../EventCategoryForUpdateDto.cs | 7 ++ EventBor.Backend.Application/Dependencies.cs | 2 + .../Mappers/MapperProfile.cs | 6 ++ .../EventCategories/EventCategoryService.cs | 98 +++++++++++++++++++ .../EventCategories/IEventCategoryService.cs | 12 +++ .../EventCategoryRepository.cs | 10 ++ .../IEventCategoryRepository.cs | 7 ++ 10 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForCreationDto.cs create mode 100644 EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForResultDto.cs create mode 100644 EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForUpdateDto.cs create mode 100644 EventBor.Backend.Application/Services/EventCategories/EventCategoryService.cs create mode 100644 EventBor.Backend.Application/Services/EventCategories/IEventCategoryService.cs create mode 100644 EventBor.Backend.Infrastructure/Database/Repositories/EventCategories/EventCategoryRepository.cs create mode 100644 EventBor.Backend.Infrastructure/Database/Repositories/EventCategories/IEventCategoryRepository.cs diff --git a/EventBor.Backend.API/appsettings.json b/EventBor.Backend.API/appsettings.json index 8f70d2a..ffd4a35 100644 --- a/EventBor.Backend.API/appsettings.json +++ b/EventBor.Backend.API/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "Host=Localhost; Port=5432; Database=EventBorDb; UserId=postgres; Password=123456@Av;" + "DefaultConnection": "Host=Localhost; Port=5432; Database=EventBorDb; UserId=postgres; Password=Mahkamov;" } } diff --git a/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForCreationDto.cs b/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForCreationDto.cs new file mode 100644 index 0000000..628c5ab --- /dev/null +++ b/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForCreationDto.cs @@ -0,0 +1,7 @@ +namespace EventBor.Backend.Application.DTOs.EventCategories; + +public class EventCategoryForCreationDto +{ + public long EventId { get; set; } + public long CategoryId { get; set; } +} diff --git a/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForResultDto.cs b/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForResultDto.cs new file mode 100644 index 0000000..197bcdf --- /dev/null +++ b/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForResultDto.cs @@ -0,0 +1,11 @@ +using EventBor.Backend.Application.DTOs.Categories; +using EventBor.Backend.Application.DTOs.Events; + +namespace EventBor.Backend.Application.DTOs.EventCategories; + +public class EventCategoryForResultDto +{ + public long Id { get; set; } + public CategoryForResultDto Category { get; set; } + public EventForResultDto Event { get; set; } +} diff --git a/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForUpdateDto.cs b/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForUpdateDto.cs new file mode 100644 index 0000000..3142358 --- /dev/null +++ b/EventBor.Backend.Application/DTOs/EventCategories/EventCategoryForUpdateDto.cs @@ -0,0 +1,7 @@ +namespace EventBor.Backend.Application.DTOs.EventCategories; + +public class EventCategoryForUpdateDto +{ + public long EventId { get; set; } + public long CategoryId { get; set; } +} diff --git a/EventBor.Backend.Application/Dependencies.cs b/EventBor.Backend.Application/Dependencies.cs index 4afa8c9..a4181c8 100644 --- a/EventBor.Backend.Application/Dependencies.cs +++ b/EventBor.Backend.Application/Dependencies.cs @@ -1,5 +1,6 @@ using EventBor.Backend.Application.Services; using EventBor.Backend.Application.Services.Categories; +using EventBor.Backend.Application.Services.EventCategories; using Microsoft.Extensions.DependencyInjection; namespace EventBor.Backend.Application; @@ -11,6 +12,7 @@ public static IServiceCollection AddApplication( { services.AddScoped(); services.AddScoped(); + services.AddScoped(); return services; } } diff --git a/EventBor.Backend.Application/Mappers/MapperProfile.cs b/EventBor.Backend.Application/Mappers/MapperProfile.cs index 79726da..6802161 100644 --- a/EventBor.Backend.Application/Mappers/MapperProfile.cs +++ b/EventBor.Backend.Application/Mappers/MapperProfile.cs @@ -1,5 +1,6 @@ using AutoMapper; using EventBor.Backend.Application.DTOs.Categories; +using EventBor.Backend.Application.DTOs.EventCategories; using EventBor.Backend.Domain.Entities; namespace EventBor.Backend.Application.Mappers; @@ -12,5 +13,10 @@ public MapperProfile() CreateMap().ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap(); + + // EventCategory + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); } } diff --git a/EventBor.Backend.Application/Services/EventCategories/EventCategoryService.cs b/EventBor.Backend.Application/Services/EventCategories/EventCategoryService.cs new file mode 100644 index 0000000..afa64d9 --- /dev/null +++ b/EventBor.Backend.Application/Services/EventCategories/EventCategoryService.cs @@ -0,0 +1,98 @@ +using AutoMapper; +using EventBor.Backend.Application.Commons.Exceptions; +using EventBor.Backend.Application.DTOs.Categories; +using EventBor.Backend.Application.DTOs.EventCategories; +using EventBor.Backend.Domain.Entities; +using EventBor.Backend.Infrastructure.Database.Repositories.Categories; +using EventBor.Backend.Infrastructure.Database.Repositories.EventCategories; +using Microsoft.EntityFrameworkCore; + +namespace EventBor.Backend.Application.Services.EventCategories; + +public class EventCategoryService : IEventCategoryService +{ + private readonly IMapper _mapper; + private readonly IEventCategoryRepository _repository; + + public EventCategoryService(IMapper mapper, IEventCategoryRepository repository) + { + _mapper = mapper; + _repository = repository; + } + + public async Task AddAsync(EventCategoryForCreationDto createDto) + { + var CheckEventId = await _repository.SelectAll() + .AsNoTracking() + .Where(e => e.EventId == createDto.EventId) + .FirstOrDefaultAsync(); + + if (CheckEventId is not null) + throw new CustomException(409, "Event already exists"); + + var CheckCategoryId = await _repository.SelectAll() + .AsNoTracking() + .Where(c => c.EventId == createDto.EventId) + .FirstOrDefaultAsync(); + + if (CheckCategoryId is not null) + throw new CustomException(409, "Category already exists"); + + var mapped = _mapper.Map (createDto); + var result = await _repository.InsertAsync(mapped); + return _mapper.Map(result); + + } + + public async Task ModifyAsync(long id, EventCategoryForUpdateDto updateDto) + { + var CheckById = await _repository.SelectAll() + .AsNoTracking() + .Where(e => e.Id == id) + .FirstOrDefaultAsync(); + + if (CheckById is null) + throw new CustomException(404, "Not Found"); + + var mapped = _mapper.Map(updateDto, CheckById); + var result = _repository.UpdateAsync(mapped); + return _mapper.Map(result); + + } + + public async Task RemoveAsync(long id) + { + var CheckById = await _repository.SelectAll() + .AsNoTracking() + .Where(e => e.Id == id) + .FirstOrDefaultAsync(); + + if (CheckById is null) + throw new CustomException(404, "Not Found"); + + await _repository.DeleteAsync(CheckById); + return true; + } + + public async Task> RetrieveAllAsync() + { + var eventcategories = await _repository.SelectAll() + .AsNoTracking() + .ToListAsync(); + + return _mapper.Map>(eventcategories); + } + + public async Task RetrieveByIdAsync(long id) + { + var CheckById = await _repository.SelectAll() + .AsNoTracking() + .Where(e => e.Id == id) + .FirstOrDefaultAsync(); + + if (CheckById is null) + throw new CustomException(404, "Not Found"); + + return _mapper.Map(CheckById); + } +} diff --git a/EventBor.Backend.Application/Services/EventCategories/IEventCategoryService.cs b/EventBor.Backend.Application/Services/EventCategories/IEventCategoryService.cs new file mode 100644 index 0000000..a3b7014 --- /dev/null +++ b/EventBor.Backend.Application/Services/EventCategories/IEventCategoryService.cs @@ -0,0 +1,12 @@ +using EventBor.Backend.Application.DTOs.EventCategories; + +namespace EventBor.Backend.Application.Services.EventCategories; + +public interface IEventCategoryService +{ + Task RemoveAsync(long id); + Task RetrieveByIdAsync(long id); + Task> RetrieveAllAsync(); + Task AddAsync(EventCategoryForCreationDto createDto); + Task ModifyAsync(long id, EventCategoryForUpdateDto updateDto); +} diff --git a/EventBor.Backend.Infrastructure/Database/Repositories/EventCategories/EventCategoryRepository.cs b/EventBor.Backend.Infrastructure/Database/Repositories/EventCategories/EventCategoryRepository.cs new file mode 100644 index 0000000..39159bd --- /dev/null +++ b/EventBor.Backend.Infrastructure/Database/Repositories/EventCategories/EventCategoryRepository.cs @@ -0,0 +1,10 @@ +using EventBor.Backend.Domain.Entities; + +namespace EventBor.Backend.Infrastructure.Database.Repositories.EventCategories; + +internal class EventCategoryRepository : Repository, IEventCategoryRepository +{ + public EventCategoryRepository(AppDbContext context) : base(context) + { + } +} diff --git a/EventBor.Backend.Infrastructure/Database/Repositories/EventCategories/IEventCategoryRepository.cs b/EventBor.Backend.Infrastructure/Database/Repositories/EventCategories/IEventCategoryRepository.cs new file mode 100644 index 0000000..24c8e8b --- /dev/null +++ b/EventBor.Backend.Infrastructure/Database/Repositories/EventCategories/IEventCategoryRepository.cs @@ -0,0 +1,7 @@ +using EventBor.Backend.Domain.Entities; + +namespace EventBor.Backend.Infrastructure.Database.Repositories.EventCategories; + +public interface IEventCategoryRepository : IRepository +{ +}