Skip to content

Commit

Permalink
Update adding and updating image in eventController
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed Jun 17, 2024
1 parent c667c9d commit 73d27fc
Showing 1 changed file with 106 additions and 63 deletions.
169 changes: 106 additions & 63 deletions Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IActionResult> AddEventImage([FromRoute] string slug, [FromForm] List<IFormFile> 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<ImageDto>();

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<IActionResult> UpdateEventImage([FromRoute] string slug, [FromForm] List<IFormFile> 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<ImageDto>();

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<ImageDto> 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<ImageDto> 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")]
Expand Down Expand Up @@ -286,13 +321,21 @@ public IActionResult GetEventsParameters([FromRoute] string slug)
[ProducesResponseType<List<TagDto>>(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);
}

Expand Down

0 comments on commit 73d27fc

Please sign in to comment.