Skip to content

Commit

Permalink
fixes and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed Jun 23, 2024
1 parent f0458a3 commit 9387f56
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 41 deletions.
80 changes: 39 additions & 41 deletions Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -113,16 +112,29 @@ public IActionResult ApproveEventRequest([FromRoute] string slug)
return Ok();
}

[HttpPost]
[Authorize(Roles = "Admin")]
[Route("{slug}/reject")]
[ProducesResponseType<EventDto>(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<IActionResult> AddEventImage([FromRoute] string slug, [FromForm] List<IFormFile> 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();
}
Expand All @@ -137,7 +149,7 @@ public async Task<IActionResult> AddEventImage([FromRoute] string slug, [FromFor

imageDtos.Add(new ImageDto
{
ObjectId = @event.Id,
ObjectId = relatedEvent.Id,
ObjectType = ObjectType.Event,
ImageData = fileBytes
});
Expand All @@ -154,9 +166,9 @@ public async Task<IActionResult> AddEventImage([FromRoute] string slug, [FromFor
public async Task<IActionResult> UpdateEventImage([FromRoute] string slug, [FromForm] List<IFormFile> 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();
}
Expand All @@ -171,18 +183,18 @@ public async Task<IActionResult> 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();
}

Expand All @@ -191,8 +203,8 @@ public async Task<IActionResult> UpdateEventImage([FromRoute] string slug, [From
[ProducesResponseType<List<string>>(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<string>();
for (int i = 0; i < count; i++)
Expand All @@ -207,15 +219,15 @@ public IActionResult GetEventImages([FromRoute] string slug)
[ProducesResponseType<FileContentResult>(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");
}
Expand Down Expand Up @@ -252,8 +264,8 @@ public IActionResult GetEventComments([FromRoute] string slug)
public IActionResult AddEventComment([FromRoute] string slug, [FromBody]CommentRequest commentRequest, [FromServices] IValidator<CommentDto> 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);
Expand All @@ -262,36 +274,22 @@ public IActionResult AddEventComment([FromRoute] string slug, [FromBody]CommentR

[HttpGet]
[Authorize(Roles = "Admin, Organizer")]
[Route("{slug}/parameters")]
[ProducesResponseType<List<ParameterDto>>(StatusCodes.Status200OK)]
public IActionResult GetEventsParameters([FromRoute] string slug)
[Route("/parameters")]
[ProducesResponseType<List<string>>(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<List<TagDto>>(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);
}

Expand Down
24 changes: 24 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Services/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ public IEnumerable<ImageDto> 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);
}

}
6 changes: 6 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public IEnumerable<ParameterDto> GetAllParameters()
.ToDtoList()
.AsEnumerable();
}
public IEnumerable<string> GetAllParameterKeys()
{
return context.Parameters
.Select(p => p.Key)
.AsEnumerable();
}

public IEnumerable<ParameterDto> GetParametersByFilter(Expression<Func<Parameter, bool>> filter)
{
Expand Down

0 comments on commit 9387f56

Please sign in to comment.