/// <summary> /// Seed random ProjectLikes into the database /// </summary> public static List <ProjectLike> SeedLikes(List <Project> projects, List <User> users) { List <ProjectLike> projectLikes = new List <ProjectLike>(); foreach (Project project in projects) { List <User> usersThatLiked = new List <User>(); Random random = new Random(); int randomLikes = random.Next(5, 15); for (int i = 0; i < randomLikes; i++) { ProjectLike projectLike = new ProjectLike { UserId = project.User.Id, LikedProject = project }; bool userFound = false; while (!userFound) { int randomUserId = random.Next(0, users.Count); User u = users[randomUserId]; if (!usersThatLiked.Contains(u)) { projectLike.ProjectLiker = u; usersThatLiked.Add(u); userFound = true; } } projectLikes.Add(projectLike); } } return(projectLikes); }
public async Task <IActionResult> UnlikeProject(int projectId) { 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 (!userProjectLikeService.CheckIfUserAlreadyLiked(currentUser.Id, projectId)) { ProblemDetails problemDetails = new ProblemDetails { Title = "User didn't like this project.", Detail = "You did not like this project at the moment.", Instance = "03590F81-C06D-4707-A646-B9B7F79B8A15" }; return(Conflict(problemDetails)); } Project projectToLike = await projectService.FindAsync(projectId); if (projectToLike == null) { ProblemDetails problemDetails = new ProblemDetails { Title = "Failed to getting the project.", Detail = "The database does not contain a project with the provided project id.", Instance = "711B2DDE-D028-479E-8CB7-33F587478F8F" }; return(NotFound(problemDetails)); } ProjectLike projectLike = new ProjectLike(projectToLike, currentUser); userProjectLikeService.Remove(projectLike); userProjectLikeService.Save(); return(Ok(mapper.Map <ProjectLike, UserProjectLikeResourceResult>(projectLike))); }
public async Task <IActionResult> LikeProject(int projectId) { 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 (userProjectLikeService.CheckIfUserAlreadyLiked(currentUser.Id, projectId)) { ProblemDetails problemDetails = new ProblemDetails { Title = "User already liked this project", Detail = "You already liked this project.", Instance = "5B0104E2-C864-4ADB-9321-32CD352DC124" }; return(Conflict(problemDetails)); } Project projectToLike = await projectService.FindAsync(projectId); if (projectToLike == null) { ProblemDetails problemDetails = new ProblemDetails { Title = "Failed to getting the project.", Detail = "The database does not contain a project with the provided project id.", Instance = "711B2DDE-D028-479E-8CB7-33F587478F8F" }; return(NotFound(problemDetails)); } try { ProjectLike like = new ProjectLike(projectToLike, currentUser); await userProjectLikeService.AddAsync(like) .ConfigureAwait(false); userProjectLikeService.Save(); return(Ok(mapper.Map <ProjectLike, UserProjectLikeResourceResult>(like))); } catch (DbUpdateException e) { Log.Logger.Error(e, "Database exception!"); ProblemDetails problemDetails = new ProblemDetails { Title = "Could not create the liked project details.", Detail = "The database failed to save the liked project.", Instance = "F941879E-6C25-4A35-A962-8E86382E1849" }; return(BadRequest(problemDetails)); } }