diff --git a/EventBor.Backend.API/Controllers/EventsController.cs b/EventBor.Backend.API/Controllers/EventsController.cs new file mode 100644 index 0000000..3aa210a --- /dev/null +++ b/EventBor.Backend.API/Controllers/EventsController.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Mvc; +using EventBor.Backend.Application.Services; +using EventBor.Backend.Application.DTOs.Events; + +namespace EventBor.Backend.API.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class EventsController : ControllerBase +{ + private readonly IEventService _eventService; + + public EventsController(IEventService eventService) + { + _eventService = eventService; + } + + [HttpPost] + public async Task PostAsync([FromBody] EventForCreationDto dto) + => Ok(await _eventService.AddAsync(dto)); + + [HttpGet] + public async Task GetAllAsync() + => Ok(await _eventService.RetrieveAllAsync()); + + [HttpGet("{id}")] + public async Task GetAsync([FromRoute(Name = "id")] long id) + => Ok(await _eventService.RetrieveByIdAsync(id)); + + + [HttpDelete("{id}")] + public async Task DeleteAsync([FromRoute(Name = "id")] long id) + => Ok(await _eventService.RemoveAsync(id)); + + + [HttpPut("{id}")] + public async Task PutAsync([FromRoute(Name = "id")] long id, [FromBody] EventForUpdateDto dto) + => Ok(await _eventService.ModifyAsync(id, dto)); +} diff --git a/EventBor.Backend.Application/DTOs/Events/EventForCreationDto.cs b/EventBor.Backend.Application/DTOs/Events/EventForCreationDto.cs new file mode 100644 index 0000000..1559d39 --- /dev/null +++ b/EventBor.Backend.Application/DTOs/Events/EventForCreationDto.cs @@ -0,0 +1,24 @@ +using EventBor.Backend.Domain.Enums; + +namespace EventBor.Backend.Application.DTOs.Events; + +public class EventForCreationDto +{ + public string Title { get; set; } + public DateTime StartedAt { get; set; } + public string Duration { get; set; } + public EventFormat Format { get; set; } + public string Platform { get; set; } + public string Banner { get; set; } + public EventStatus Status { get; set; } + public string Orginizer { get; set; } + public string Description { get; set; } + public string Location { get; set; } + public string Address { get; set; } + public bool IsPaid { get; set; } + public decimal Price { get; set; } + public string RegistrationLink { get; set; } + public int Capacity { get; set; } + public string Contact { get; set; } + public string OfficialPage { get; set; } +} diff --git a/EventBor.Backend.Application/DTOs/Events/EventForResultDto.cs b/EventBor.Backend.Application/DTOs/Events/EventForResultDto.cs new file mode 100644 index 0000000..8f67b07 --- /dev/null +++ b/EventBor.Backend.Application/DTOs/Events/EventForResultDto.cs @@ -0,0 +1,26 @@ +using EventBor.Backend.Domain.Enums; +using Microsoft.AspNetCore.Http; + +namespace EventBor.Backend.Application.DTOs.Events; + +public class EventForResultDto +{ + public long Id { get; set; } + public string Title { get; set; } + public DateTime StartedAt { get; set; } + public string Duration { get; set; } + public EventFormat Format { get; set; } + public string Platform { get; set; } + public IFormFile Banner { get; set; } + public EventStatus Status { get; set; } + public string Orginizer { get; set; } + public string Description { get; set; } + public string Location { get; set; } + public string Address { get; set; } + public bool IsPaid { get; set; } + public decimal Price { get; set; } + public string RegistrationLink { get; set; } + public int Capacity { get; set; } + public string Contact { get; set; } + public string OfficialPage { get; set; } +} diff --git a/EventBor.Backend.Application/DTOs/Events/EventForUpdateDto.cs b/EventBor.Backend.Application/DTOs/Events/EventForUpdateDto.cs new file mode 100644 index 0000000..570c064 --- /dev/null +++ b/EventBor.Backend.Application/DTOs/Events/EventForUpdateDto.cs @@ -0,0 +1,24 @@ +using EventBor.Backend.Domain.Enums; + +namespace EventBor.Backend.Application.DTOs.Events; + +public class EventForUpdateDto +{ + public string Title { get; set; } + public DateTime StartedAt { get; set; } + public string Duration { get; set; } + public EventFormat Format { get; set; } + public string Platform { get; set; } + public string Banner { get; set; } + public EventStatus Status { get; set; } + public string Orginizer { get; set; } + public string Description { get; set; } + public string Location { get; set; } + public string Address { get; set; } + public bool IsPaid { get; set; } + public decimal Price { get; set; } + public string RegistrationLink { get; set; } + public int Capacity { get; set; } + public string Contact { get; set; } + public string OfficialPage { get; set; } +} diff --git a/EventBor.Backend.Application/Dependencies.cs b/EventBor.Backend.Application/Dependencies.cs index 4afa8c9..662262c 100644 --- a/EventBor.Backend.Application/Dependencies.cs +++ b/EventBor.Backend.Application/Dependencies.cs @@ -10,7 +10,9 @@ public static IServiceCollection AddApplication( this IServiceCollection services) { services.AddScoped(); + services.AddScoped(); services.AddScoped(); + return services; } } diff --git a/EventBor.Backend.Application/EventBor.Backend.Application.csproj b/EventBor.Backend.Application/EventBor.Backend.Application.csproj index 0cd1ca8..c9d9fdd 100644 --- a/EventBor.Backend.Application/EventBor.Backend.Application.csproj +++ b/EventBor.Backend.Application/EventBor.Backend.Application.csproj @@ -7,6 +7,8 @@ + + diff --git a/EventBor.Backend.Application/Mappers/MapperProfile.cs b/EventBor.Backend.Application/Mappers/MapperProfile.cs index 79726da..c5bc9b5 100644 --- a/EventBor.Backend.Application/Mappers/MapperProfile.cs +++ b/EventBor.Backend.Application/Mappers/MapperProfile.cs @@ -1,6 +1,7 @@ using AutoMapper; -using EventBor.Backend.Application.DTOs.Categories; using EventBor.Backend.Domain.Entities; +using EventBor.Backend.Application.DTOs.Events; +using EventBor.Backend.Application.DTOs.Categories; namespace EventBor.Backend.Application.Mappers; @@ -8,9 +9,15 @@ public class MapperProfile : Profile { public MapperProfile() { + // Event + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + // Category CreateMap().ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap(); + } } diff --git a/EventBor.Backend.Application/Services/EventService.cs b/EventBor.Backend.Application/Services/EventService.cs new file mode 100644 index 0000000..8a31bcb --- /dev/null +++ b/EventBor.Backend.Application/Services/EventService.cs @@ -0,0 +1,86 @@ +using AutoMapper; +using Microsoft.EntityFrameworkCore; +using EventBor.Backend.Domain.Entities; +using EventBor.Backend.Application.DTOs.Events; +using EventBor.Backend.Application.Commons.Exceptions; +using EventBor.Backend.Infrastructure.Database.Repositories; + +namespace EventBor.Backend.Application.Services; + +public class EventService : IEventService +{ + private readonly IMapper _mapper; + private readonly IEventRepository _eventRepository; + + public EventService(IMapper mapper, IEventRepository eventRepository) + { + _mapper = mapper; + _eventRepository = eventRepository; + } + public async Task AddAsync(EventForCreationDto dto) + { + var eventData = await _eventRepository + .SelectAll() + .Where(e => e.Title.ToLower() == dto.Title.ToLower()) + .FirstOrDefaultAsync(); + + if(eventData is not null) + throw new CustomException(409,"Event is already exist"); + + var mappedEventData = _mapper.Map(dto); + + return _mapper.Map(await _eventRepository.InsertAsync(mappedEventData)); + } + + public async Task ModifyAsync(long id, EventForUpdateDto dto) + { + var eventData = await _eventRepository + .SelectAll() + .Where(e => e.Id == id) + .FirstOrDefaultAsync(); + + if(eventData is null) + throw new CustomException(404,"Event not found"); + + var mappedEventData = _mapper.Map(dto, eventData); + return _mapper.Map(_eventRepository.UpdateAsync(mappedEventData)); + } + + public async Task RemoveAsync(long id) + { + var eventData = await _eventRepository + .SelectAll() + .Where(e => e.Id == id) + .FirstOrDefaultAsync(); + if (eventData is null) + throw new CustomException(404,"Event not found"); + + await _eventRepository.DeleteAsync(eventData); + + return true; + } + + public async Task> RetrieveAllAsync() + { + var allEventData = await _eventRepository + .SelectAll() + .AsNoTracking() + .FirstOrDefaultAsync(); + + return _mapper.Map>(allEventData); + } + + public async Task RetrieveByIdAsync(long id) + { + var eventData = await _eventRepository + .SelectAll() + .Where(e => e.Id == id) + .AsNoTracking() + .FirstOrDefaultAsync(); + + if (eventData is null) + throw new CustomException(404, "Event is not found"); + + return _mapper.Map(eventData); + } +} diff --git a/EventBor.Backend.Application/Services/IEventService.cs b/EventBor.Backend.Application/Services/IEventService.cs new file mode 100644 index 0000000..7d62835 --- /dev/null +++ b/EventBor.Backend.Application/Services/IEventService.cs @@ -0,0 +1,12 @@ +using EventBor.Backend.Application.DTOs.Events; + +namespace EventBor.Backend.Application.Services; + +public interface IEventService +{ + Task RemoveAsync(long id); + Task RetrieveByIdAsync(long id); + Task> RetrieveAllAsync(); + Task AddAsync(EventForCreationDto dto); + Task ModifyAsync(long id, EventForUpdateDto dto); +} diff --git a/EventBor.Backend.Domain/Entities/User.cs b/EventBor.Backend.Domain/Entities/User.cs index 2301a47..cd3470c 100644 --- a/EventBor.Backend.Domain/Entities/User.cs +++ b/EventBor.Backend.Domain/Entities/User.cs @@ -1,3 +1,4 @@ + using EventBor.Backend.Domain.Entities.Commons; using EventBor.Backend.Domain.Enums; using System.Text.Json.Serialization; diff --git a/EventBor.Backend.Infrastructure/Database/Repositories/EventRepository.cs b/EventBor.Backend.Infrastructure/Database/Repositories/EventRepository.cs new file mode 100644 index 0000000..0cf6db2 --- /dev/null +++ b/EventBor.Backend.Infrastructure/Database/Repositories/EventRepository.cs @@ -0,0 +1,9 @@ +using EventBor.Backend.Domain.Entities; + +namespace EventBor.Backend.Infrastructure.Database.Repositories; + +internal class EventRepository : Repository, IEventRepository +{ + public EventRepository(AppDbContext context) : base(context) + { } +} diff --git a/EventBor.Backend.Infrastructure/Database/Repositories/IEventRepository.cs b/EventBor.Backend.Infrastructure/Database/Repositories/IEventRepository.cs new file mode 100644 index 0000000..c3faf40 --- /dev/null +++ b/EventBor.Backend.Infrastructure/Database/Repositories/IEventRepository.cs @@ -0,0 +1,7 @@ +using EventBor.Backend.Domain.Entities; + +namespace EventBor.Backend.Infrastructure.Database.Repositories; + +public interface IEventRepository : IRepository +{ +} diff --git a/EventBor.Backend.Infrastructure/Dependencies.cs b/EventBor.Backend.Infrastructure/Dependencies.cs index 47658bf..34f9591 100644 --- a/EventBor.Backend.Infrastructure/Dependencies.cs +++ b/EventBor.Backend.Infrastructure/Dependencies.cs @@ -1,9 +1,9 @@ -using EventBor.Backend.Infrastructure.Database; -using EventBor.Backend.Infrastructure.Database.Repositories; -using EventBor.Backend.Infrastructure.Database.Repositories.Categories; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using EventBor.Backend.Infrastructure.Database; using Microsoft.Extensions.DependencyInjection; +using EventBor.Backend.Infrastructure.Database.Repositories; +using EventBor.Backend.Infrastructure.Database.Repositories.Categories; namespace EventBor.Backend.Infrastructure; @@ -19,8 +19,11 @@ public static IServiceCollection AddInfrastructure( services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); + services.AddScoped(); + services.AddScoped(); services.AddScoped(); + return services; } }