diff --git a/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs b/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs index a3449703..217a2019 100644 --- a/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs +++ b/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs @@ -127,68 +127,103 @@ public IActionResult ApproveEventRequest([FromRoute] string slug) eventToApprove.Status = EventStatus.Approved; _eventService.UpdateEvent(eventToApprove.Id, eventToApprove.ToDto()); return Ok(eventToApprove); + } + + [HttpPost] + [Authorize(Roles = "Admin, Organizer")] + [Route("{slug}/images")] + public async Task AddEventImage([FromRoute] string slug, [FromForm] List images) + { + var @event = _eventService.GetEventBySlug(slug); + var user = _userService.GetCurrentUser(); + + if (@event.OrgenizerId != user.Id && user.Role != UserRole.Admin) + { + return Forbid(); + } + + var imageDtos = new List(); + + foreach (var formFile in images) + { + if (formFile.Length > 0) + { + using (var ms = new MemoryStream()) + { + await formFile.CopyToAsync(ms); + var fileBytes = ms.ToArray(); + + imageDtos.Add(new ImageDto + { + ObjectId = @event.Id, + ObjectType = ObjectType.Event, + ImageData = fileBytes + }); + } + } + } + + var image = _imageService.CreateImage(imageDtos, ObjectType.Event); + return Ok(image); + } + + [HttpPut] + [Authorize(Roles = "Admin, Organizer")] + [Route("{slug}/images/{imageId:int}")] + public async Task UpdateEventImage([FromRoute] string slug, [FromForm] List images) + { + var user = _userService.GetCurrentUser(); + var @event = _eventService.GetEventBySlug(slug); + + if (@event.OrgenizerId != user.Id && user.Role != UserRole.Admin) + { + return Forbid(); + } + + var imageDtos = new List(); + + foreach (var formFile in images) + { + if (formFile.Length > 0) + { + using (var ms = new MemoryStream()) + { + await formFile.CopyToAsync(ms); + var fileBytes = ms.ToArray(); + + imageDtos.Add(new ImageDto + { + ObjectId = @event.Id, + ObjectType = ObjectType.Event, + ImageData = fileBytes + }); + } + } + } + + if (@event.Id != imageDtos[0].ObjectId) + { + return NotFound(); + } + + var image = _imageService.UpdateImages(imageDtos, ObjectType.Event); + return Ok(image); } - [HttpPost] - [Authorize(Roles = "Admin, Organizer")] - [Route("{slug}/images")] - public IActionResult AddEventImage([FromRoute] string slug, [FromBody] List imageDtos) - { - var @event = _eventService.GetEventBySlug(slug); - var user = _userService.GetCurrentUser(); - - if (@event.OrgenizerId != user.Id && user.Role != UserRole.Admin) - { - return Forbid(); - } - - if (@event.Id != imageDtos[0].ObjectId) - { - return NotFound(); - } - - var image = _imageService.CreateImage(imageDtos, ObjectType.Event); - return Ok(image); - - } - - [HttpPut] - [Authorize(Roles = "Admin, Organizer")] - [Route("{slug}/images/{imageId:int}")] - public IActionResult UpdateEventImage([FromRoute] string slug,[FromBody] List imageDtos) - { - var user = _userService.GetCurrentUser(); - var @event = _eventService.GetEventBySlug(slug); - - if (@event.OrgenizerId != user.Id && user.Role != UserRole.Admin) - { - return Forbid(); - } - - if (@event.Id != imageDtos[0].ObjectId) - { - return NotFound(); - } - var image = _imageService.UpdateImages(imageDtos, ObjectType.Event); - return Ok(image); - - } - - [HttpDelete] - [Authorize(Roles = "Admin, Organizer")] - [Route("{slug}/images/{imageId:int}")] - public IActionResult DeleteEventImage([FromRoute] string slug, [FromRoute] int imageId) - { - var user = _userService.GetCurrentUser(); - var @event = _eventService.GetEventBySlug(slug); - - if (@event.OrgenizerId == user.Id) - { - return Forbid(); - } - - throw new NotImplementedException(); // pytanie czy to potrzebne zadziala na updatcie XD - } + //[HttpDelete] + //[Authorize(Roles = "Admin, Organizer")] + //[Route("{slug}/images/{imageId:int}")] + //public IActionResult DeleteEventImage([FromRoute] string slug, [FromRoute] int imageId) + //{ + // var user = _userService.GetCurrentUser(); + // var @event = _eventService.GetEventBySlug(slug); + + // if (@event.OrgenizerId == user.Id) + // { + // return Forbid(); + // } + // // pytanie czy to potrzebne zadziala na updatcie XD + //} //[HttpGet] //[Route("{slug}/users")] @@ -286,13 +321,21 @@ public IActionResult GetEventsParameters([FromRoute] string slug) [ProducesResponseType>(StatusCodes.Status200OK)] public IActionResult GetEventsTags([FromRoute] string slug) { - var eventDto = _eventService.GetEventBySlug(slug).ToDto(); - if (eventDto == null) + var user = _userService.GetCurrentUser(); + var @event = _eventService.GetEventBySlug(slug); + + if (user.Role != UserRole.Admin && @event.OrganizerId != user.Id) + { + Forbid(); + } + + if (@event == null) { return NotFound(); } - var tags = eventDto.Tags; + + var tags = @event.Tags; return Ok(tags); }