Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/389 direct communication #465

Merged
merged 24 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b3995a2
Merge pull request #1 from DigitalExcellence/develop
ZeroHackz Mar 3, 2021
0e36027
acl + cleanup collecties
nora1410xd Mar 5, 2021
c24d627
global + warmup collections
nora1410xd Mar 5, 2021
ec8658b
Revert "global + warmup collections"
ZeroHackz Mar 5, 2021
7183170
Revert "acl + cleanup collecties"
ZeroHackz Mar 5, 2021
9e4fefd
Merge pull request #2 from DigitalExcellence/develop
Zarthenix Apr 14, 2021
d54101e
Seed update
Zarthenix Apr 23, 2021
baf5b3b
Merge pull request #3 from DigitalExcellence/develop
Zarthenix Apr 23, 2021
86d3bcb
First iteration comment-system
Zarthenix May 12, 2021
e982172
Merge pull request #4 from DigitalExcellence/develop
ZeroHackz Jun 4, 2021
99ff63f
Merge branch 'develop' into feature/389-direct-communication
ZeroHackz Jun 4, 2021
3b2859c
migrations
nora1410xd Jun 4, 2021
34ec28f
Update ProjectController.cs
nora1410xd Jun 4, 2021
4994f40
:adhesive_bandage: add datetime to comment
nora1410xd Jun 10, 2021
6035d3b
:adhesive_bandage: include user on get comment
nora1410xd Jun 10, 2021
3ad73be
:recycle: corrected update/created comment
nora1410xd Jun 11, 2021
5e50bcf
update commentresourcemodel
nora1410xd Jun 11, 2021
84fe748
Add UpdateProjectComment
nora1410xd Jun 16, 2021
b9cd9b6
projectlike repository + service
nora1410xd Jun 16, 2021
a68821b
Create UserProjectCommentLikeResourceResult.cs
nora1410xd Jun 16, 2021
caf2768
Update ProjectController.cs
nora1410xd Jun 16, 2021
67909c6
Fixed toevoegen like
nora1410xd Jun 16, 2021
18fb8d0
Update ProjectController.cs
nora1410xd Jun 16, 2021
4b2cd5e
Update ProjectCommentRepository.cs
nora1410xd Jun 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions API/Configuration/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public MappingProfile()
destination.LikedProject.Description))
.ForAllOtherMembers(member => member.Ignore());

CreateMap<ProjectCommentLike, UserProjectCommentLikeResourceResult>()
.ForMember(source => source.Id,
option => option.
MapFrom(destination =>
destination.LikedComment.Id))
.ForMember(source => source.comment,
option => option
.MapFrom(destination =>
destination.LikedComment.Content))
.ForAllOtherMembers(member => member.Ignore());
CreateMap<UserProjectCommentLikeResourceResult, ProjectCommentLike>();

CreateMap<UserUserResourceResult, UserUser>();

CreateMap<UserUser, UserUserResourceResult>()
Expand Down Expand Up @@ -115,6 +127,10 @@ public MappingProfile()
.ForMember(q => q.Id, opt => opt.MapFrom(q=> q.Category.Id))
.ForMember(q => q.Name, opt => opt.MapFrom(q => q.Category.Name));

CreateMap<ProjectCommentResource, ProjectComment>();
CreateMap<ProjectComment, ProjectCommentResourceResult>();


CreateMap<EmbeddedProjectResource, EmbeddedProject>();
CreateMap<EmbeddedProject, EmbeddedProjectResourceResult>();

Expand Down
212 changes: 211 additions & 1 deletion API/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ public class ProjectController : ControllerBase
private readonly IProjectService projectService;
private readonly IProjectCategoryService projectCategoryService;
private readonly ICategoryService categoryService;
private readonly IProjectCommentService projectCommentService;
private readonly IUserProjectLikeService userProjectLikeService;
private readonly IUserProjectService userProjectService;
private readonly IUserService userService;
private readonly IProjectInstitutionService projectInstitutionService;
private readonly IInstitutionService institutionService;
private readonly IUserProjectCommentLikeService userProjectCommentLikeService;
/// <summary>
/// Initializes a new instance of the <see cref="ProjectController" /> class
/// </summary>
Expand All @@ -84,6 +86,8 @@ public class ProjectController : ControllerBase
/// <param name="callToActionOptionService">The call to action option service is used to communicate with the logic layer.</param>
/// <param name="categoryService">The category service is used to work with categories</param>
/// <param name="projectCategoryService">The project category service is used to connect projects and categories</param>
/// <param name="projectCommentService"></param>
/// <param name="userProjectCommentLikeService"></param>
/// <param name="projectInstitutionService">The projectinstitution service is responsible for link projects and institutions.</param>
/// <param name="institutionService">The institution service which is used to communicate with the logic layer</param>
public ProjectController(IProjectService projectService,
Expand All @@ -98,7 +102,9 @@ public ProjectController(IProjectService projectService,
IInstitutionService institutionService,
ICallToActionOptionService callToActionOptionService,
ICategoryService categoryService,
IProjectCategoryService projectCategoryService)
IProjectCategoryService projectCategoryService,
IProjectCommentService projectCommentService,
IUserProjectCommentLikeService userProjectCommentLikeService)
{
this.projectService = projectService;
this.userService = userService;
Expand All @@ -113,6 +119,8 @@ public ProjectController(IProjectService projectService,
this.projectCategoryService = projectCategoryService;
this.projectInstitutionService = projectInstitutionService;
this.institutionService = institutionService;
this.projectCommentService = projectCommentService;
this.userProjectCommentLikeService = userProjectCommentLikeService;
}


Expand Down Expand Up @@ -1399,6 +1407,208 @@ await projectCategoryService.RemoveAsync(alreadyCategorized.Id)

return Ok(mapper.Map<Project, ProjectResourceResult>(project));
}

/// <summary>
/// Gets the comments belonging to the provided project-id.
/// </summary>
/// <param name="projectId">The project-id for which to retrieve the comments.</param>
/// <returns></returns>
[HttpGet("comments/{projectId}")]
[AllowAnonymous]
[ProducesResponseType(typeof(List<ProjectCommentResourceResult>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetCommentsAsync(int projectId)
{
if(projectId < 0)
{
ProblemDetails problem = new ProblemDetails
{
Title = "Failed getting comments",
Detail =
"The projectId is smaller than 0 and therefore it could never retrieve comments.",
Instance = ""
};
return BadRequest(problem);
}

List<ProjectComment> comments = await projectCommentService.GetProjectComments(projectId);

if(comments == null)
{
ProblemDetails problem = new ProblemDetails
{
Title = "Failed getting comments",
Detail = "The comments could not be found in the database.",
Instance = ""

};
return NotFound(problem);
}

return Ok(mapper.Map<List<ProjectComment>, List<ProjectCommentResourceResult>>(comments));
}

/// <summary>
/// Post a new comment to the provided project-id
/// </summary>
/// <param name="projectId">The project-id to add the new comment to</param>
/// <param name="projectCommentResource"></param>
/// <param name="comment">The added comment and its metadata</param>
/// <returns></returns>
[HttpPost("comment/{projectId}")]
[Authorize]
[ProducesResponseType(typeof(ProjectCommentResourceResult), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> AddCommentToProject(int projectId, [FromBody] ProjectCommentResource projectCommentResource)
{
Project project = await projectService.FindAsync(projectId).ConfigureAwait(false);
if(project == null)
{
ProblemDetails problem = new ProblemDetails
{
Title = "Failed to add comment to the project.",
Detail = "The project could not be found in the database.",
Instance = "1C8D069D-E6CE-43E2-9CF9-D82C0A71A293"
};
return NotFound(problem);
}
if(projectCommentResource.Content == null || projectCommentResource.UserId == null || projectCommentResource.Content == "")
{
ProblemDetails problem = new ProblemDetails
{
Title = "Failed to add comment to the project.",
Detail = "Not all necessary Fields are filled in.",
Instance = "1C8D069D-E6CE-43E2-9CF9-D82C0A71A294"
};
return BadRequest(problem);
}


ProjectComment projectComment = mapper.Map<ProjectCommentResource, ProjectComment>(projectCommentResource);
projectComment.User = await HttpContext.GetContextUser(userService).ConfigureAwait(false);
projectComment.ProjectId = projectId;

try
{
projectCommentService.Add(projectComment);
projectCommentService.Save();
return Created(nameof(AddCommentToProject), mapper.Map<ProjectComment,ProjectCommentResourceResult>(projectComment));
}catch(DbUpdateException e)
{
Log.Logger.Error(e, "Database exception");
ProblemDetails problem = new ProblemDetails
{
Title = "Failed to add comment to the project.",
Detail = "There was a problem while saving thecomment.",
Instance = "1C8D069D-E6CE-43E2-9CF9-D82C0A71A292"
};
return BadRequest(problem);
}
}

/// <summary>
/// this method is responsible for updating the comment with the specified identifier.
/// </summary>
/// <param name="projectCommentId"></param>
/// <param name="projectCommentResource"></param>
/// <returns></returns>
[HttpPut("comment/{projectCommentId}")]
[Authorize]
public async Task<IActionResult> UpdateComment(int projectCommentId, [FromBody] ProjectCommentResource projectCommentResource)
{
ProjectComment projectComment = await projectCommentService.FindAsync(projectCommentId);
if(projectComment == null)
{
ProblemDetails problem = new ProblemDetails
{
Title = "Failed to update Comment.",
Detail = "The specified Comment could not be found in the database.",
Instance = "b27d3600-33b0-42a0-99aa-4b2f28ea07bb"
};
return NotFound(problem);

}
User user = await HttpContext.GetContextUser(userService).ConfigureAwait(false);
bool isAllowed = userService.UserHasScope(user.IdentityId, nameof(Defaults.Scopes.AdminProjectWrite));

if(!(projectComment.User.Id == user.Id || isAllowed))
{
ProblemDetails problem = new ProblemDetails
{
Title = "Failed to edit the comment.",
Detail = "The user is not allowed to edit the comment.",
Instance = "906cd8ad-b75c-4efb-9838-849f99e8026b"
};
return Unauthorized(problem);
}
mapper.Map(projectCommentResource, projectComment);
projectCommentService.Update(projectComment);
projectService.Save();
return Ok(mapper.Map<ProjectComment, ProjectCommentResourceResult>(projectComment));

}
[HttpPost("comment/like/{projectCommentId}")]
[Authorize]
public async Task<IActionResult> LikeProjectComment(int projectCommentId)
{
User currentUser = await HttpContext.GetContextUser(userService)
.ConfigureAwait(false);

if(currentUser == null)
{
ProblemDetails problemDetails = new ProblemDetails
{
Title = "Failed to getting the user account.",
Detail =
"The database does not contain a user with the provided user id.",
Instance = "F8DB2F94-48DA-4FEB-9BDA-FF24A59333C1"
};
return NotFound(problemDetails);
}
if(userProjectCommentLikeService.CheckIfUserAlreadyLiked(currentUser.Id, projectCommentId))
{
ProblemDetails problemDetails = new ProblemDetails
{
Title = "User already liked this comment",
Detail = "You already liked this comment.",
Instance = "5B0104E2-C864-4ADB-9321-32CD352DC124"
};
return Conflict(problemDetails);
}
ProjectComment projectCommentToLike = await projectCommentService.FindAsync(projectCommentId);
if(projectCommentToLike == null)
{
ProblemDetails problemDetails = new ProblemDetails
{
Title = "Failed to getting the comment.",
Detail = "The database does not contain a comment with the provided comment id.",
Instance = "711B2DDE-D028-479E-8CB7-33F587478F8F"
};
return NotFound(problemDetails);
}
try
{
ProjectCommentLike projectCommentLike = new ProjectCommentLike(projectCommentToLike, currentUser);
await userProjectCommentLikeService.AddAsync(projectCommentLike).ConfigureAwait(false);
userProjectCommentLikeService.Save();

return Ok(mapper.Map< ProjectCommentLike, UserProjectCommentLikeResourceResult>(projectCommentLike));
}catch(DbUpdateException e)
{
Log.Logger.Error(e, "database exception");

ProblemDetails problemDetails = new ProblemDetails
{
Title = "Could not create the liked comment details.",
Detail = "The database failed to save the liked comment.",
Instance = "F941879E-6C25-4A35-A962-8E86382E1849"
};
return BadRequest(problemDetails);
}

}
}

}
6 changes: 6 additions & 0 deletions API/Extensions/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public static IServiceCollection AddServicesAndRepositories(this IServiceCollect
services.AddScoped<ICategoryService, CategoryService>();
services.AddScoped<ICategoryRepository, CategoryRepository>();

services.AddScoped<IProjectCommentService, ProjectCommentService>();
services.AddScoped<IProjectCommentRepository, ProjectCommentRepository>();

services.AddScoped<IProjectCategoryService, ProjectCategoryService>();
services.AddScoped<IProjectCategoryRepository, ProjectCategoryRepository>();

Expand Down Expand Up @@ -112,6 +115,9 @@ public static IServiceCollection AddServicesAndRepositories(this IServiceCollect
services.AddScoped<IUserProjectLikeService, UserProjectLikeService>();
services.AddScoped<IUserProjectLikeRepository, UserProjectLikeRepository>();

services.AddScoped<IUserProjectCommentLikeService, UserProjectCommentLikeService>();
services.AddScoped<IUserProjectCommentLikeRepository, UserProjectCommentLikeRepository>();

services.AddScoped<ICallToActionOptionService, CallToActionOptionService>();
services.AddScoped<ICallToActionOptionRepository, CallToActionOptionRepository>();

Expand Down
36 changes: 25 additions & 11 deletions API/HelperClasses/SeedHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,36 @@ public static List<RoleScope> FindRoleScopesNotInDb(List<RoleScope> seededRoleSc
/// </summary>
/// <param name="seedUser"></param>
/// <param name="context"></param>
public static void InsertUser(User seedUser, ApplicationDbContext context)
public static void InsertUsers(List<User> seedUsers, ApplicationDbContext context)
{
List<User> usersInDb = context.User.AsQueryable()
foreach(User seedUser in seedUsers)
{
List<User> usersInDb = context.User.AsQueryable()
.Include(e => e.Role)
.ToList();

if(usersInDb.Find(e => e.IdentityId == seedUser.IdentityId) != null)
{
User foundEntity = usersInDb.Find(e => e.IdentityId == seedUser.IdentityId);
foundEntity.Role = seedUser.Role;
context.Update(foundEntity);
context.SaveChanges();
return;
if(usersInDb.Find(e => e.IdentityId == seedUser.IdentityId) != null)
{
User foundEntity = usersInDb.Find(e => e.IdentityId == seedUser.IdentityId);
foundEntity.Role = seedUser.Role;
context.Update(foundEntity);
context.SaveChanges();
return;
}
if(usersInDb.Find(e => e.Name == seedUser.Name) != null || usersInDb.Find(e => e.Email == seedUser.Email) != null)
{

User foundEntity = usersInDb.Find(e => e.Name == seedUser.Name || e.Email == seedUser.Email);
foundEntity.Role = seedUser.Role;
foundEntity.IdentityId = seedUser.IdentityId;
foundEntity.Name = seedUser.Name;
foundEntity.Email = seedUser.Email;
context.Update(foundEntity);
context.SaveChanges();
return;
}
context.User.Add(seedUser);
}

context.User.Add(seedUser);
context.SaveChanges();
}

Expand Down
23 changes: 23 additions & 0 deletions API/Resources/ProjectCommentResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace API.Resources
{
public class ProjectCommentResource
{
public int Id { get; set; }

public string Username { get; set; }

public int UserId { get; set; }

public DateTime Created { get; set; }

public DateTime Updated { get; set; }

public string Content { get; set; }

}
}
24 changes: 24 additions & 0 deletions API/Resources/ProjectCommentResourceResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace API.Resources
{
public class ProjectCommentResourceResult
{

public int Id { get; set; }

public User User { get; set; }

public DateTime Created { get; set; }

public DateTime Updated { get; set; }

public string Content { get; set; }

public List<ProjectCommentLike> Likes { get; set; }
}
}
Loading