Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated EventController
Browse files Browse the repository at this point in the history
bilimig committed Jun 22, 2024
1 parent 722c787 commit 7807e0e
Showing 8 changed files with 95 additions and 72 deletions.
10 changes: 5 additions & 5 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ public void UpdateEvent_EventExists_EventUpdated()
mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List<Parameter>());
mockContext.Setup(c => c.Comments).ReturnsDbSet(new List<Comment>());
mockContext.Setup(c => c.Participants).ReturnsDbSet(new List<Participant>());
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

var result = eventService.UpdateEvent(1, eventDto);
Assert.AreEqual("name1", result.Name);
@@ -142,7 +142,7 @@ public void UpdateEvent_EventDoesNotExist_NullReturned()
UpdatedAt =DateTime.Now
}});

var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

Assert.ThrowsException<NotFoundException>(() => eventService.UpdateEvent(1, eventDto));
}
@@ -203,7 +203,7 @@ public void GetEventById_EventExists_EventReturned()
UpdatedAt =DateTime.Now }});


var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

var result = eventService.GetEventById(1);
Assert.IsNotNull(result);
@@ -248,7 +248,7 @@ public void GetEventById_EventDoesNotExist_NullReturned()
}});


var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

Assert.ThrowsException<NotFoundException>(() => eventService.GetEventById(1));
}
@@ -318,7 +318,7 @@ public void DeleteEvent_EventExists_EventDeleted()
mockContext.Setup(c => c.Comments).ReturnsDbSet(new List<Comment>());
mockContext.Setup(c => c.Participants).ReturnsDbSet(new List<Participant>());

var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

eventService.DeleteEvent(1);

61 changes: 21 additions & 40 deletions Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
@@ -28,20 +28,8 @@ public class EventsController(
public IActionResult GetEvents()
{
var events = eventService.GetAllEvents();
var eventsResponses = new List<EventResponse>();

foreach (var thisEvent in events)
{
var participating = eventService.GetEventParticipantsCountBySlugAndStatus(thisEvent.Slug, ParticipantStatus.Participating);
var interested = eventService.GetEventParticipantsCountBySlugAndStatus(thisEvent.Slug, ParticipantStatus.Interested);
var relatedEvent = eventService.GetEventBySlug(thisEvent.Slug);
var username = userService.GetUserUsernameById(relatedEvent.OrganizerId);
var participants = new Participants(participating, interested);
var eventResponse = thisEvent.ToResponse(participants, username, $"/api/v1/Users/image/{username}", relatedEvent.Address.ToDto(), relatedEvent.AddressId);
eventsResponses.Add(eventResponse);
}

return Ok(eventsResponses);
return Ok(events);
}

[HttpPost]
@@ -51,10 +39,10 @@ public IActionResult CreateEvent([FromBody] EventCreateRequest eventRequest, [Fr
{
var user = userService.GetCurrentUser();

var address = addressService.CreateAddress(eventRequest.AddressDto);
var eventDto = eventRequest.ToDto(user.Id);
eventDto.Slug = "temp";
validator.ValidateAndThrow(eventDto);
eventService.CreateEvent(eventDto);
eventService.CreateEvent(eventDto, eventRequest.AddressDto);
return Created();
}

@@ -63,12 +51,13 @@ public IActionResult CreateEvent([FromBody] EventCreateRequest eventRequest, [Fr
[ProducesResponseType<EventResponse>(StatusCodes.Status200OK)]
public IActionResult GetEventBySlug([FromRoute] string slug)
{
var realatedEvent = eventService.GetEventBySlug(slug);
var relatedEvent = eventService.GetEventBySlug(slug);
var participating = eventService.GetEventParticipantsCountBySlugAndStatus(slug, ParticipantStatus.Participating);
var interested = eventService.GetEventParticipantsCountBySlugAndStatus(slug, ParticipantStatus.Interested);
var username = userService.GetUserUsernameById(realatedEvent.OrganizerId);
var username = relatedEvent.Organizer.Username;
var participants = new Participants(participating, interested);
var eventResponse = realatedEvent.ToDto().ToResponse(participants, username, $"/api/v1/Users/image/{username}", realatedEvent.Address.ToDto(), realatedEvent.AddressId);
var images = eventService.GetEventImages(slug);
var eventResponse = relatedEvent.ToDto().ToResponse(participants, username, $"/api/v1/Users/image/{username}", relatedEvent.Address.ToDto(), relatedEvent.AddressId, images);

return Ok(eventResponse);
}
@@ -82,7 +71,8 @@ public IActionResult UpdateEvent([FromRoute] string slug, [FromBody] EventUpdate

var existingEvent = eventService.GetEventBySlug(slug);
var user = userService.GetCurrentUser();
var eventDto = eventUpdateRequest.ToDto();
var eventDto = eventUpdateRequest.ToDto(user.Id);
eventDto.Slug = slug;
validator.ValidateAndThrow(eventDto);
if (existingEvent.OrganizerId != user.Id && user.Role != UserRole.Admin)
{
@@ -106,18 +96,8 @@ public IActionResult UpdateEvent([FromRoute] string slug, [FromBody] EventUpdate
public IActionResult GetEventsRequests()
{
var events = eventService.GetEventsByFilter(e => e.Status == EventStatus.PendingApproval);
var eventsDtos = new List<EventResponse>();
foreach (var thisEvent in events)
{
var participating = eventService.GetEventParticipantsCountBySlugAndStatus(thisEvent.Slug, ParticipantStatus.Participating);
var interested = eventService.GetEventParticipantsCountBySlugAndStatus(thisEvent.Slug, ParticipantStatus.Interested);
var realtedEvent = eventService.GetEventBySlug(thisEvent.Slug);
var username = userService.GetUserUsernameById(realtedEvent.OrganizerId);
var participants = new Participants(participating, interested);
var eventResponse = thisEvent.ToResponse(participants, username, $"/api/v1/Users/image/{username}", realtedEvent.Address.ToDto(),realtedEvent.AddressId);
eventsDtos.Add(eventResponse);
}
return Ok(eventsDtos);

return Ok(events);
}

[HttpPost]
@@ -272,27 +252,28 @@ public IActionResult GetEventComments([FromRoute] string slug)
public IActionResult AddEventComment([FromRoute] string slug, [FromBody]CommentRequest commentRequest, [FromServices] IValidator<CommentDto> validator)
{
var user = userService.GetCurrentUser();

var commentDto = commentRequest.ToDtoFromRequest(user.Id);
var @event = eventService.GetEventBySlug(slug);
var commentDto = commentRequest.ToDtoFromRequest(user.Id, @event.Id);
validator.ValidateAndThrow(commentDto);

eventService.AddEventComment(commentDto,slug);
eventService.AddEventComment(commentDto);
return Ok();
}

[HttpGet]
[Authorize(Roles = "Admin, Organizer")]
[Route("parameters")]
[Route("{slug}/parameters")]
[ProducesResponseType<List<ParameterDto>>(StatusCodes.Status200OK)]
public IActionResult GetEventsParameters([FromRoute] string slug)
{
var eventDto = eventService.GetEventBySlug(slug).ToDto();
if (eventDto == null)
var @event = eventService.GetEventBySlug(slug);
var user = userService.GetCurrentUser();
if (user.Role != UserRole.Admin && @event.OrganizerId != user.Id)
{
return NotFound();
Forbid();
}

var parameters = eventDto.Parameters;
var parameters = @event.Parameters.ToDtoList();
return Ok(parameters);
}

@@ -310,7 +291,7 @@ public IActionResult GetEventsTags([FromRoute] string slug)
Forbid();
}

var tags = @event.Tags;
var tags = @event.Tags.ToDtoList();
return Ok(tags);
}

4 changes: 2 additions & 2 deletions Server/ReasnAPI/ReasnAPI/Mappers/CommentMapper.cs
Original file line number Diff line number Diff line change
@@ -33,11 +33,11 @@ public static Comment ToEntity(this CommentDto commentDto)
};
}

public static CommentDto ToDtoFromRequest(this CommentRequest commentRequest, int userId)
public static CommentDto ToDtoFromRequest(this CommentRequest commentRequest, int userId, int eventId)
{
return new CommentDto()
{
EventId = commentRequest.EventId,
EventId = eventId,
Content = commentRequest.Content,
CreatedAt = DateTime.Now,
UserId = userId,
9 changes: 5 additions & 4 deletions Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ public static Event ToEntity(this EventDto eventDto)
};
}

public static EventResponse ToResponse(this EventDto eventDto, Participants participants, string username, string image, AddressDto addressDto, int addressId)
public static EventResponse ToResponse(this EventDto eventDto, Participants participants, string username, string image, AddressDto addressDto, int addressId, List<string> images)
{
var organizer = new Organizer(username, image);

@@ -65,16 +65,17 @@ public static EventResponse ToResponse(this EventDto eventDto, Participants part
Tags = eventDto.Tags,
Parameters = eventDto.Parameters,
AddressDto = addressDto,
Participants = participants
Participants = participants,
Images = images
};
}

public static EventDto ToDto(this EventUpdateRequest eventCreateRequest)
public static EventDto ToDto(this EventUpdateRequest eventCreateRequest, int organizerId)
{
return new EventDto()
{
Name = eventCreateRequest.Name,
OrganizerId = eventCreateRequest.OrganizerId,
OrganizerId = organizerId,
Description = eventCreateRequest.Description,
StartAt = eventCreateRequest.StartAt,
EndAt = eventCreateRequest.EndAt,
2 changes: 0 additions & 2 deletions Server/ReasnAPI/ReasnAPI/Models/API/CommentRequest.cs
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@
{
public class CommentRequest
{
public int EventId { get; set; }
public string Content { get; set; } = null!;
public DateTime CreatedAt { get; set; }
}
}
1 change: 1 addition & 0 deletions Server/ReasnAPI/ReasnAPI/Models/API/EventResponse.cs
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ public class EventResponse()
public List<TagDto>? Tags { get; set; }
public List<ParameterDto>? Parameters { get; set; }
public Participants? Participants { get; set; }
public List<string>? Images { get; set; }
}

}
1 change: 0 additions & 1 deletion Server/ReasnAPI/ReasnAPI/Models/API/EventUpdateRequest.cs
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ public class EventUpdateRequest
{
public string Name { get; set; } = null!;
public AddressDto AddressDto { get; set; } = null!;
public int OrganizerId { get; set; }
public string Description { get; set; } = null!;
public DateTime StartAt { get; set; }
public DateTime EndAt { get; set; }
79 changes: 61 additions & 18 deletions Server/ReasnAPI/ReasnAPI/Services/EventService.cs
Original file line number Diff line number Diff line change
@@ -6,12 +6,14 @@
using System.Transactions;
using System.Text.RegularExpressions;
using ReasnAPI.Mappers;
using ReasnAPI.Models.API;
using ReasnAPI.Models.Enums;

namespace ReasnAPI.Services;
public class EventService(ReasnContext context, ParameterService parameterService, TagService tagService, CommentService commentService, AddressService addressService)
public class EventService(ReasnContext context, ParameterService parameterService, TagService tagService, CommentService commentService, AddressService addressService, ImageService imageService)
{
public EventDto CreateEvent(EventDto eventDto)

public EventDto CreateEvent(EventDto eventDto, AddressDto addressDto)
{
using (var scope = new TransactionScope())
{
@@ -20,10 +22,13 @@ public EventDto CreateEvent(EventDto eventDto)
newEvent.CreatedAt = createdTime;
newEvent.UpdatedAt = createdTime;
newEvent.Slug = CreateSlug(eventDto);


addressService.CreateAddress(addressDto);
newEvent.Address = addressDto.ToEntity();
context.SaveChanges();
context.Events.Add(newEvent);


context.SaveChanges();
if (eventDto.Tags != null)
{
@@ -36,7 +41,7 @@ public EventDto CreateEvent(EventDto eventDto)
var newParameters = eventDto.Parameters.ToEntityList();
parameterService.AttachParametersToEvent(newParameters, newEvent);
}

context.SaveChanges();
scope.Complete();
eventDto.Slug = newEvent.Slug;
@@ -62,7 +67,6 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto)

eventToUpdate.Name = eventDto.Name;
eventToUpdate.Description = eventDto.Description;
eventToUpdate.OrganizerId = eventDto.OrganizerId;
eventToUpdate.StartAt = eventDto.StartAt;
eventToUpdate.EndAt = eventDto.EndAt;
eventToUpdate.UpdatedAt = DateTime.UtcNow;
@@ -168,7 +172,7 @@ public EventDto GetEventById(int eventId)

public Event GetEventBySlug(string slug)
{
var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e => e.Address).FirstOrDefault(e => e.Slug == slug);
var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e => e.Address).Include(e => e.Organizer).FirstOrDefault(e => e.Slug == slug);
if (eventToReturn is null)
{
throw new NotFoundException("Event not found");
@@ -218,27 +222,66 @@ public IEnumerable<CommentDto> GetEventCommentsBySlug(string slug)
return commentDtos;
}

public void AddEventComment(CommentDto commentDto, string slug)
public void AddEventComment(CommentDto commentDto)
{
commentDto = commentService.CreateComment(commentDto);
var relatedEvent = GetEventBySlug(slug);
relatedEvent.Comments.Add(commentDto.ToEntity());
commentService.CreateComment(commentDto);
context.SaveChanges();
}

public IEnumerable<EventDto> GetEventsByFilter(Expression<Func<Event, bool>> filter)
public IEnumerable<EventResponse> GetEventsByFilter(Expression<Func<Event, bool>> filter)
{
var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).Include(e => e.Address).Include(e => e.Organizer).Where(filter).ToList();

var eventsResponses = new List<EventResponse>();
foreach (var thisEvent in events)
{
var participating = GetEventParticipantsCountBySlugAndStatus(thisEvent.Slug, ParticipantStatus.Participating);
var interested = GetEventParticipantsCountBySlugAndStatus(thisEvent.Slug, ParticipantStatus.Interested);
var participants = new Participants(participating, interested);
var username = thisEvent.Organizer.Username;
var addressDto = thisEvent.Address.ToDto();
var addressId = thisEvent.AddressId;

var eventDto = thisEvent.ToDto();
var eventResponse = eventDto.ToResponse(participants, username, $"/api/v1/Users/image/{username}", addressDto, addressId, GetEventImages(thisEvent.Slug));
eventsResponses.Add(eventResponse);
}

return eventsResponses.AsEnumerable();
}

public List<string> GetEventImages(string slug)
{
var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).Where(filter).ToList();
var eventDtos = events.ToDtoList();
return eventDtos;
var @event = GetEventBySlug(slug);
var images = imageService.GetImagesByEventId(@event.Id);
var count = images.Count();
var stringList = new List<string>();
for (int i = 0; i < count; i++)
{
stringList.Add($"/api/v1/Events/{slug}/image/{i}");
}
return stringList;
}

public IEnumerable<EventDto> GetAllEvents()
public IEnumerable<EventResponse> GetAllEvents()
{
var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).ToList();
var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).Include(e => e.Address).Include(e => e.Organizer).ToList();
var eventsResponses = new List<EventResponse>();
foreach (var thisEvent in events )
{
var participating = GetEventParticipantsCountBySlugAndStatus(thisEvent.Slug, ParticipantStatus.Participating);
var interested = GetEventParticipantsCountBySlugAndStatus(thisEvent.Slug, ParticipantStatus.Interested);
var participants = new Participants(participating, interested);
var username = thisEvent.Organizer.Username;
var addressDto = thisEvent.Address.ToDto();
var addressId = thisEvent.AddressId;

var eventDto = thisEvent.ToDto();
var eventResponse = eventDto.ToResponse(participants, username, $"/api/v1/Users/image/{username}", addressDto, addressId, GetEventImages(thisEvent.Slug));
eventsResponses.Add(eventResponse);
}

var eventDtos = events.ToDtoList();
return eventDtos;
return eventsResponses.AsEnumerable();
}
public IEnumerable<EventDto> GetUserEvents(string username)
{

0 comments on commit 7807e0e

Please sign in to comment.