Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EventBor.Backend.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EventBor.Backend.Application.DTOs.EventCategories;

public class EventCategoryForCreationDto
{
public long EventId { get; set; }
public long CategoryId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EventBor.Backend.Application.DTOs.EventCategories;

public class EventCategoryForUpdateDto
{
public long EventId { get; set; }
public long CategoryId { get; set; }
}
2 changes: 2 additions & 0 deletions EventBor.Backend.Application/Dependencies.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +12,7 @@ public static IServiceCollection AddApplication(
{
services.AddScoped<IUserService, UserService>();
services.AddScoped<ICategoryService, CategoryService>();
services.AddScoped<IEventCategoryService, EventCategoryService>();
return services;
}
}
6 changes: 6 additions & 0 deletions EventBor.Backend.Application/Mappers/MapperProfile.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,5 +13,10 @@ public MapperProfile()
CreateMap<Category, CategoryForResultDto>().ReverseMap();
CreateMap<Category, CategoryForUpdateDto>().ReverseMap();
CreateMap<Category, CategoryForCreationDto>().ReverseMap();

// EventCategory
CreateMap<EventCategory, EventCategoryForCreationDto>().ReverseMap();
CreateMap<EventCategory, EventCategoryForUpdateDto>().ReverseMap();
CreateMap<EventCategory, EventCategoryForResultDto>().ReverseMap();
}
}
Original file line number Diff line number Diff line change
@@ -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<EventCategoryForResultDto> 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<EventCategory> (createDto);
var result = await _repository.InsertAsync(mapped);
return _mapper.Map<EventCategoryForResultDto>(result);

}

public async Task<EventCategoryForResultDto> 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<EventCategoryForResultDto>(result);

}

public async Task<bool> 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<IEnumerable<EventCategoryForResultDto>> RetrieveAllAsync()
{
var eventcategories = await _repository.SelectAll()
.AsNoTracking()
.ToListAsync();

return _mapper.Map<IEnumerable<EventCategoryForResultDto>>(eventcategories);
}

public async Task<EventCategoryForResultDto> 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<EventCategoryForResultDto>(CheckById);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using EventBor.Backend.Application.DTOs.EventCategories;

namespace EventBor.Backend.Application.Services.EventCategories;

public interface IEventCategoryService
{
Task<bool> RemoveAsync(long id);
Task<EventCategoryForResultDto> RetrieveByIdAsync(long id);
Task<IEnumerable<EventCategoryForResultDto>> RetrieveAllAsync();
Task<EventCategoryForResultDto> AddAsync(EventCategoryForCreationDto createDto);
Task<EventCategoryForResultDto> ModifyAsync(long id, EventCategoryForUpdateDto updateDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using EventBor.Backend.Domain.Entities;

namespace EventBor.Backend.Infrastructure.Database.Repositories.EventCategories;

internal class EventCategoryRepository : Repository<EventCategory>, IEventCategoryRepository
{
public EventCategoryRepository(AppDbContext context) : base(context)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using EventBor.Backend.Domain.Entities;

namespace EventBor.Backend.Infrastructure.Database.Repositories.EventCategories;

public interface IEventCategoryRepository : IRepository<EventCategory>
{
}