public void DeleteShouldDeleteAGivenComment() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(DeleteShouldDeleteAGivenComment)) .Options; using (var context = new TasksDbContext(options)) { var commentsService = new CommentsService(context); var toAdd = new CommentPostDTO() { Important = true, Text = "A nice task...", }; var actual = commentsService.Create(toAdd, null); var afterDelete = commentsService.Delete(actual.Id); int numberOfCommentsInDb = context.Comments.CountAsync().Result; var resultComment = context.Comments.Find(actual.Id); Assert.IsNotNull(afterDelete); Assert.IsNull(resultComment); Assert.AreEqual(0, numberOfCommentsInDb); } }
public void UpsertShouldModifyTheGivenComment() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(UpsertShouldModifyTheGivenComment)) .Options; using (var context = new TasksDbContext(options)) { var commentsService = new CommentsService(context); var toAdd = new CommentPostDTO() { Important = true, Text = "A nice task...", }; var added = commentsService.Create(toAdd, null); context.Entry(added).State = EntityState.Detached; var update = new Comment() { Important = false, Text = "A nice task...", }; var updateResult = commentsService.Upsert(added.Id, update); Assert.NotNull(updateResult); Assert.False(updateResult.Important); Assert.AreEqual(added.Text, updateResult.Text); } }
public void GetByIdShouldReturnCommentWithCorrectId() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnCommentWithCorrectId)) .Options; using (var context = new TasksDbContext(options)) { var commentsService = new CommentsService(context); var toAdd = new CommentPostDTO() { Important = true, Text = "A nice task...", }; var current = commentsService.Create(toAdd, null); var expected = commentsService.GetById(current.Id); Assert.IsNotNull(expected); Assert.AreEqual(expected.Text, current.Text); Assert.AreEqual(expected.Id, current.Id); } }
public Comment Create(CommentPostDTO comment, User addedBy) { Comment commentAdd = CommentPostDTO.ToComment(comment); commentAdd.Owner = addedBy; context.Comments.Add(commentAdd); context.SaveChanges(); return(commentAdd); }
public void CreateShouldAddAndReturnTheCreatedComment() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(CreateShouldAddAndReturnTheCreatedComment)) .Options; using (var context = new TasksDbContext(options)) { var commentsService = new CommentsService(context); var toAdd = new CommentPostDTO() { Important = true, Text = "A nice task...", }; var added = commentsService.Create(toAdd, null); Assert.IsNotNull(added); Assert.AreEqual("A nice task...", added.Text); Assert.True(added.Important); } }
public IHttpActionResult Add([FromBody] CommentPostDTO comment) { int Id = 0; if (ModelState.IsValid) { try { CommentEntity commentDomain = _DTOAssempler.CreateCommentEntity(comment); ICommentServices commentService = ServiceFactory.getCommentServices(); Id = commentService.Add(commentDomain); } catch (Exception e) { return(BadRequest(e.Message)); } } else { return(BadRequest("Neispravni podaci")); } return(Ok(Id)); }
public void InitInformation(InfoDTO info) { ServiceRepository serviceRepository = new ServiceRepository(); page.placeName.Text = info.place; page.vrijemeOd.Text = "Od: " + info.startTime.ToString(); page.vrijemeDo.Text = "Do: " + info.endTime.ToString(); page.opis.Text = info.content; page.likesCount.Text = info.reputation.ToString(); page.upvote.Tapped += new TappedEventHandler(async delegate(object o, TappedRoutedEventArgs e) { await Vote(true, info); } ); page.downvote.Tapped += new TappedEventHandler(async delegate(object o, TappedRoutedEventArgs e) { await Vote(false, info); } ); page.commentButton.Click += new RoutedEventHandler(async delegate(object o, RoutedEventArgs e) { page.commentButton.IsEnabled = false; page.commentTextbox.IsEnabled = false; CommentPostDTO comment = new CommentPostDTO() { userId = SessionManager.SessionID, content = page.commentTextbox.Text, contentInfoId = info.Id, time = DateTime.Now }; int?id = await serviceRepository.setComment(comment); if (id != null) { List <CommentDTO> tempList = new List <CommentDTO>(info.comments); tempList.Add(new CommentDTO() { Id = (int)id, content = comment.content, user = "******", reputation = 0, time = comment.time }); info.comments = tempList; } InitComments(info); page.commentButton.IsEnabled = true; page.commentTextbox.IsEnabled = true; } ); InitComments(info); }
public async Task <int?> setComment(CommentPostDTO postDTO) { return(await fetchObject <int?>(@"/comment/add", "POST", postDTO)); }
public void Post([FromBody] CommentPostDTO comment) { User addedBy = usersService.GetCurrentUser(HttpContext); commentsService.Create(comment, addedBy); }
public async Task <IHttpActionResult> PostFeedback(CommentPostDTO comment) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Project project = await db.Projects.FindAsync(comment.project_id); if (project == null) { return(StatusCode(HttpStatusCode.NotFound)); } string reg = User.Identity.GetUserId(); Student student = await db.Students.Where(d => d.registrationId == reg).SingleOrDefaultAsync(); Company company = await db.Companies.Where(d => d.registrationId == reg).SingleOrDefaultAsync(); Feedback b; Int32 timeStamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; DateTime date = DateTime.UtcNow; if (student != null) { string middle_name = student.middleName; if (middle_name == null) { middle_name = ""; } Feedback feedback = new Feedback() { projectId = comment.project_id, comment = comment.comment, clientId = student.Id, type = "student", date = "" + date.Month + "" + date.Day + "" + date.Year, name = student.firstName + " " + middle_name + " " + student.lastName, profile_picture = student.profilePic, time = timeStamp }; b = db.Feedbacks.Add(feedback); //return comment to client await db.SaveChangesAsync(); CommentDTO new_comment = new CommentDTO { Id = b.Id, comment = b.comment, date = b.date, client = new ClientDTO() { clientId = b.clientId, name = b.name, profile_picture = b.profile_picture, type = b.type } }; return(Ok(new_comment)); } else if (company != null) { Feedback feedback = new Feedback() { projectId = comment.project_id, comment = comment.comment, clientId = company.Id, name = company.companyName, profile_picture = company.profilePicture, type = "company", date = "" + date.Month + "" + date.Day + "" + date.Year, time = timeStamp }; b = db.Feedbacks.Add(feedback); await db.SaveChangesAsync(); CommentDTO new_comment = new CommentDTO { Id = b.Id, comment = b.comment, date = b.date, client = new ClientDTO() { clientId = b.clientId, name = b.name, profile_picture = b.profile_picture, type = b.type } }; return(Ok(new_comment)); } else { return(StatusCode(HttpStatusCode.ExpectationFailed)); } }