Skip to content

Commit

Permalink
[RSN-19] - Create Controllers (#79)
Browse files Browse the repository at this point in the history
* test: add unit tests for jwt, handlers and validators

* feat: add placeholders for endpoints

* chore: add missing controllers placeholders

* Restored GetCurrentUser method

* [RSN-60] - Implement required methods in MeController and UserController (#58)

* feat: add jwt and exceptions handlers

Simplify the logic on the controllers' side, custom exceptions and
a library for validation were added. The implemented handlers are
allow to catch those exceptions and return the corresponding
statuses and detailed response.

Additionally, mappers have been created to more easily convert
entities into the corresponding DTOs, fixed enum conversion
when it comes to UserRole and export postgres port on the
development environment.

* test: add unit tests for jwt, handlers and validators

* feat: add placeholders for endpoints

* chore: add missing controllers placeholders

* Created controllers

* Created controllers

* Finished controllers and updated services

* Update UserService tests

* Create and update image methods update

* Image controllers update

* Update controller and service

* Added Address to UserDto

* Controller update and service changes

Changed Image and Participant service (and participant tests), controller now use new service methods

* Methods name changes

---------

Co-authored-by: raczu <[email protected]>

* [RSN-59] - Created EventsController (#59)

* feat: add jwt and exceptions handlers

Simplify the logic on the controllers' side, custom exceptions and
a library for validation were added. The implemented handlers are
allow to catch those exceptions and return the corresponding
statuses and detailed response.

Additionally, mappers have been created to more easily convert
entities into the corresponding DTOs, fixed enum conversion
when it comes to UserRole and export postgres port on the
development environment.

* test: add unit tests for jwt, handlers and validators

* feat: add placeholders for endpoints

* chore: add missing controllers placeholders

* Created EventsController

* Little Updates

* Updated controllers

* Update adding and updating image in eventController

* Updated eventcontroller logic

* Updated EventsController

* Fixed build issues

* updated eventscontroller

* fixed build issue

addedscope for address service

* Update EventServicesTest.cs

* Update EventsController.cs

* syntax update

* updated models

changed removing tags and params that are not attached to any event, thay will be hold in database

* Update EventServicesTest.cs

* Updated EventController

* updated events controller

added returnign empty list if image for event is not found, filtered get all events to not return waiting for aproval events

* updated logic in getallaevenst

it wont return events where status is receted canceled and panding aproval

* fixes and updates

* Fixes and cleanup after rebase and confilts

* Update EventService.cs

* updates

* Updated logic with slag on creation

* Update EventValidatorTests.cs

* Update EventValidatorTests.cs

---------

Co-authored-by: raczu <[email protected]>

* Controller and service update

Return user's image for comment

---------

Co-authored-by: raczu <[email protected]>
Co-authored-by: Maciej Koperdowski <[email protected]>
Co-authored-by: Maciej Koperdowski <[email protected]>
  • Loading branch information
4 people authored Jun 23, 2024
1 parent 0584936 commit 554e9d0
Show file tree
Hide file tree
Showing 38 changed files with 1,487 additions and 620 deletions.
165 changes: 20 additions & 145 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/CommentServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,13 @@
using ReasnAPI.Models.DTOs;
using ReasnAPI.Models.Enums;
using ReasnAPI.Services;
using System.Runtime.Versioning;

namespace ReasnAPI.Tests.Services;

[TestClass]
public class CommentServiceTests
{
[TestMethod]
public void GetCommentById_CommentExist_CommentReturned()
{
var mockContext = new Mock<ReasnContext>();

var event1 = new Event
{
Id = 1,
Name = "Event",
Description = "Description",
};

var user = new User
{
Id = 1,
Username = "Username",
Email = "Email",
Password = "Password",
};

var comment = new Comment
{
Id = 1,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
};

var fakeComment = new FakeDbSet<Comment> { comment };
var fakeEvent = new FakeDbSet<Event> { event1 };
var fakeUser = new FakeDbSet<User> { user };

mockContext.Setup(c => c.Users).Returns(fakeUser);
mockContext.Setup(c => c.Events).Returns(fakeEvent);
mockContext.Setup(c => c.Comments).Returns(fakeComment);

var commentService = new CommentService(mockContext.Object);

var result = commentService.GetCommentById(1);

Assert.IsNotNull(result);
Assert.AreEqual("Content", result.Content);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
}

[TestMethod]
public void GetCommentById_CommentDoesNotExist_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Comments).ReturnsDbSet([]);

var commentService = new CommentService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => commentService.GetCommentById(1));
}

[TestMethod]
public void GetAllComments_CommentsExist_CommentsReturned()
{
Expand All @@ -86,22 +30,23 @@ public void GetAllComments_CommentsExist_CommentsReturned()
Id = 1,
Name = "Event",
Description = "Description",
Slug = "Slug"
};

var comment1 = new Comment
{
Id = 1,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
Event = event1,
User = user
};

var comment2 = new Comment
{
Id = 2,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
Event = event1,
User = user
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
Expand Down Expand Up @@ -148,22 +93,25 @@ public void GetCommentsByFilter_CommentsExist_CommentsReturned()
Id = 1,
Name = "Event",
Description = "Description",
Slug = "Slug"
};

var comment1 = new Comment
{
Id = 1,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
Event = event1,
EventId = 1,
User = user
};

var comment2 = new Comment
{
Id = 2,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
Event = event1,
EventId = 1,
User = user
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
Expand Down Expand Up @@ -210,6 +158,7 @@ public void CreateComment_CommentCreated_CommentReturned()
Id = 1,
Name = "Event",
Description = "Description",
Slug = "Slug",
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
Expand All @@ -221,16 +170,16 @@ public void CreateComment_CommentCreated_CommentReturned()
var commentDto = new CommentDto
{
Content = "Content",
UserId = 1,
EventId = 1
Username = user.Username,
EventSlug = event1.Slug,
};

var result = commentService.CreateComment(commentDto);
var result = commentService.CreateComment(commentDto, event1.Id, user.Id);

Assert.IsNotNull(result);
Assert.AreEqual("Content", result.Content);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
Assert.AreEqual("Slug", result.EventSlug);
Assert.AreEqual("Username", result.Username);
}

[TestMethod]
Expand All @@ -241,81 +190,7 @@ public void CreateComment_CommentDtoIsNull_NullReturned()

var commentService = new CommentService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => commentService.CreateComment(null));
}

[TestMethod]
public void UpdateComment_CommentUpdated_CommentReturned()
{
var mockContext = new Mock<ReasnContext>();

var user = new User
{
Id = 1,
Username = "Username",
Email = "Email",
Password = "Password",
};

var event1 = new Event
{
Id = 1,
Name = "Event",
Description = "Description",
};

var comment = new Comment
{
Id = 1,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
mockContext.Setup(c => c.Events).ReturnsDbSet([event1]);
mockContext.Setup(c => c.Comments).ReturnsDbSet([comment]);

var commentService = new CommentService(mockContext.Object);

var result = commentService.UpdateComment(1, new CommentDto
{
Content = "UpdatedContent",
EventId = 2,
UserId = 1
});

Assert.IsNotNull(result);
Assert.AreEqual("UpdatedContent", result.Content);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
}

[TestMethod]
public void UpdateComment_CommentDoesNotExist_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Comments).ReturnsDbSet([]);

var commentService = new CommentService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => commentService.UpdateComment(1, new CommentDto
{
Content = "UpdatedContent",
EventId = 2,
UserId = 1
}));
}

[TestMethod]
public void UpdateComment_CommentDtoIsNull_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Comments).ReturnsDbSet([]);

var commentService = new CommentService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => commentService.UpdateComment(1, null));
Assert.ThrowsException<ArgumentNullException>(() => commentService.CreateComment(null, 1, 1));
}

[TestMethod]
Expand Down
12 changes: 5 additions & 7 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public void UpdateEvent_EventExists_EventUpdated()
var eventDto = new EventDto()
{
Name = "name1",
AddressId = 1,
Description = "description2",
OrganizerId = 1,
StartAt = DateTime.Now,
Expand Down Expand Up @@ -82,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));
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);
Expand All @@ -102,7 +101,6 @@ public void UpdateEvent_EventDoesNotExist_NullReturned()
var eventDto = new EventDto()
{
Name = "name1",
AddressId = 1,
Description = "description2",
OrganizerId = 1,
StartAt = DateTime.Now,
Expand Down Expand Up @@ -144,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));
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));
}
Expand Down Expand Up @@ -205,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));
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);
Expand Down Expand Up @@ -250,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));
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));
}
Expand Down Expand Up @@ -320,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));
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);

Expand Down
Loading

0 comments on commit 554e9d0

Please sign in to comment.