diff --git a/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs b/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs index aa2ce5b..473096e 100644 --- a/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs +++ b/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs @@ -14,11 +14,10 @@ namespace ReasnAPI.Controllers; [ApiController] [Route("[controller]")] public class EventsController( - ReasnContext context, EventService eventService, UserService userService, ImageService imageService, - AddressService addressService, + ParameterService parameterService, TagService tagService) : ControllerBase { @@ -113,16 +112,29 @@ public IActionResult ApproveEventRequest([FromRoute] string slug) return Ok(); } + [HttpPost] + [Authorize(Roles = "Admin")] + [Route("{slug}/reject")] + [ProducesResponseType(StatusCodes.Status200OK)] + public IActionResult RejectEventRequest([FromRoute] string slug) + { + var eventToApprove = eventService.GetEventBySlug(slug); + + eventToApprove.Status = EventStatus.Rejected; + eventService.UpdateEvent(eventToApprove.Id, eventToApprove.ToDto()); + return Ok(); + } + [HttpPost] [Authorize(Roles = "Admin, Organizer")] [Route("{slug}/images")] [ProducesResponseType(StatusCodes.Status201Created)] public async Task AddEventImage([FromRoute] string slug, [FromForm] List images) { - var @event = eventService.GetEventBySlug(slug); + var relatedEvent = eventService.GetEventBySlug(slug); var user = userService.GetCurrentUser(); - if (@event.OrganizerId != user.Id && user.Role != UserRole.Admin) + if (relatedEvent.OrganizerId != user.Id && user.Role != UserRole.Admin) { return Forbid(); } @@ -137,7 +149,7 @@ public async Task AddEventImage([FromRoute] string slug, [FromFor imageDtos.Add(new ImageDto { - ObjectId = @event.Id, + ObjectId = relatedEvent.Id, ObjectType = ObjectType.Event, ImageData = fileBytes }); @@ -154,9 +166,9 @@ public async Task AddEventImage([FromRoute] string slug, [FromFor public async Task UpdateEventImage([FromRoute] string slug, [FromForm] List images) { var user = userService.GetCurrentUser(); - var @event = eventService.GetEventBySlug(slug); + var relatedEvent = eventService.GetEventBySlug(slug); - if (@event.OrganizerId != user.Id && user.Role != UserRole.Admin) + if (relatedEvent.OrganizerId != user.Id && user.Role != UserRole.Admin) { return Forbid(); } @@ -171,18 +183,18 @@ public async Task UpdateEventImage([FromRoute] string slug, [From imageDtos.Add(new ImageDto { - ObjectId = @event.Id, + ObjectId = relatedEvent.Id, ObjectType = ObjectType.Event, ImageData = fileBytes }); } - if (@event.Id != imageDtos[0].ObjectId) + if (relatedEvent.Id != imageDtos[0].ObjectId) { return NotFound(); } - imageService.UpdateImagesForEvent(@event.Id, imageDtos); + imageService.UpdateImagesForEvent(relatedEvent.Id, imageDtos); return Ok(); } @@ -191,8 +203,8 @@ public async Task UpdateEventImage([FromRoute] string slug, [From [ProducesResponseType>(StatusCodes.Status200OK)] public IActionResult GetEventImages([FromRoute] string slug) { - var @event = eventService.GetEventBySlug(slug); - var images = imageService.GetImagesByEventId(@event.Id); + var relatedEvent = eventService.GetEventBySlug(slug); + var images = imageService.GetImagesByEventId(relatedEvent.Id); var count = images.Count(); var stringList = new List(); for (int i = 0; i < count; i++) @@ -207,15 +219,15 @@ public IActionResult GetEventImages([FromRoute] string slug) [ProducesResponseType(StatusCodes.Status200OK)] public IActionResult GetEventImage([FromRoute] string slug, [FromRoute] int id) { - var @event = eventService.GetEventBySlug(slug); - var images = imageService.GetImagesByEventId(@event.Id).ToList(); + var relatedEvent = eventService.GetEventBySlug(slug); + var count = imageService.GetImageCountByEventId(relatedEvent.Id); - if (images.Count <= id) + if (count <= id) { return NotFound(); } - var image = images[id]; + var image = imageService.GetImageByEventIdAndIndex(relatedEvent.Id,id); return File(image.ImageData, $"image/jpeg"); } @@ -252,8 +264,8 @@ public IActionResult GetEventComments([FromRoute] string slug) public IActionResult AddEventComment([FromRoute] string slug, [FromBody]CommentRequest commentRequest, [FromServices] IValidator validator) { var user = userService.GetCurrentUser(); - var @event = eventService.GetEventBySlug(slug); - var commentDto = commentRequest.ToDtoFromRequest(user.Id, @event.Id); + var relatedEvent = eventService.GetEventBySlug(slug); + var commentDto = commentRequest.ToDtoFromRequest(user.Id, relatedEvent.Id); validator.ValidateAndThrow(commentDto); eventService.AddEventComment(commentDto); @@ -262,36 +274,22 @@ public IActionResult AddEventComment([FromRoute] string slug, [FromBody]CommentR [HttpGet] [Authorize(Roles = "Admin, Organizer")] - [Route("{slug}/parameters")] - [ProducesResponseType>(StatusCodes.Status200OK)] - public IActionResult GetEventsParameters([FromRoute] string slug) + [Route("/parameters")] + [ProducesResponseType>(StatusCodes.Status200OK)] + public IActionResult GetEventsParameters() { - var @event = eventService.GetEventBySlug(slug); - var user = userService.GetCurrentUser(); - if (user.Role != UserRole.Admin && @event.OrganizerId != user.Id) - { - Forbid(); - } - - var parameters = @event.Parameters.ToDtoList(); + var parameters = parameterService.GetAllParameterKeys().ToList(); return Ok(parameters); } [HttpGet] - [Authorize(Roles = "Admin, Organizer")] - [Route("{slug}/tags")] + [Authorize(Roles = "Admin, Organizer\"")] + [Route("/tags")] [ProducesResponseType>(StatusCodes.Status200OK)] - public IActionResult GetEventsTags([FromRoute] string slug) + public IActionResult GetEventsTags() { - var user = userService.GetCurrentUser(); - var @event = eventService.GetEventBySlug(slug); - - if (user.Role != UserRole.Admin && @event.OrganizerId != user.Id) - { - Forbid(); - } - - var tags = @event.Tags.ToDtoList(); + + var tags = tagService.GetAllTags().ToList(); return Ok(tags); } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index e169cd5..f1d5dd0 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -218,6 +218,30 @@ public IEnumerable GetImagesByEventId(int eventId) var imageDtos = images.ToDtoList().AsEnumerable(); return imageDtos; + } + + public ImageDto GetImageByEventIdAndIndex(int eventId, int index) + { + + var image = context.Images + .Where(image => image.ObjectType == ObjectType.Event && image.ObjectId == eventId) + .Skip(index) + .FirstOrDefault(); + + if (image == null) + { + throw new NotFoundException($"Image at index {index} not found for eventId {eventId}"); + } + + var imageDto = image.ToDto(); + + return imageDto; + } + + public int GetImageCountByEventId(int eventId) + { + return context.Images + .Count(image => image.ObjectType == ObjectType.Event && image.ObjectId == eventId); } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index f38c853..6e0cb86 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -144,6 +144,12 @@ public IEnumerable GetAllParameters() .ToDtoList() .AsEnumerable(); } + public IEnumerable GetAllParameterKeys() + { + return context.Parameters + .Select(p => p.Key) + .AsEnumerable(); + } public IEnumerable GetParametersByFilter(Expression> filter) {