public IHttpActionResult PostComments([FromBody]UserCommentsAPI userComment) { string userName = User.Identity.Name; if (String.IsNullOrEmpty(userName)) { return BadRequest("You can not comment, please register or sing in!"); } string userId = ""; try { userId = db.Users.SingleOrDefault(x => x.Email == userName).Id; } catch { userId = "NONE"; } userComment.ApplicationUserId = userId; userComment.UserName = userName; if (CheckUser(userComment) == false) { return BadRequest("Posting comment is not allowed. Please check arrival date, payment or you have not visited this accommodation!"); } else if (IsAlreadyCommented(userComment) == true) { return BadRequest("You have already commented this accomodation!"); } UserComment userCommentForSave = new UserComment(); userCommentForSave.AccomodationId = userComment.AccomodationId; userCommentForSave.ApplicationUserId = userComment.ApplicationUserId; userCommentForSave.Comment = userComment.Comment; userCommentForSave.Rating = userComment.Rating; userCommentForSave.UserName = userComment.UserName; db.UserComments.Add(userCommentForSave); db.SaveChanges(); return Ok("Thank you for Your comment. Have a nice day."); }
public void UserCommentsCreateInvalidInput() { UserCommentsController controller = new UserCommentsController(); UserComment uc = new UserComment(); uc.UserCommentId = 1; uc.AccomodationId = 1; uc.Comment = "Some new comment"; uc.ApplicationUserId = "12345"; uc.UserName = "******"; uc.Rating = -2; uc.ReportCount = -5; var context = new ValidationContext(uc, null, null); var results = new List<ValidationResult>(); TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(UserComment), typeof(UserComment)), typeof(UserComment)); var isModelStateValid = Validator.TryValidateObject(uc, context, results, true); Assert.AreEqual(false, isModelStateValid); }
private bool CheckUser(UserComment userComment) { int userCounts = 0; try { userCounts = db.RoomAvailabilities.Count(x => x.UserId == userComment.ApplicationUserId && x.ArrivalDate < DateTime.Now && x.AccomodationId == userComment.AccomodationId); } catch { return false; } if (userCounts == 1) { return true; } return false; }
private bool IsAlreadyCommented(UserComment userComment) { try { double test = db.UserComments.SingleOrDefault(x => x.AccomodationId == userComment.AccomodationId && x.ApplicationUserId == userComment.ApplicationUserId).Rating; } catch { return false; } return true; }
public IHttpActionResult PostUserComment(UserComment userComment) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.UserComments.Add(userComment); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = userComment.UserCommentId }, userComment); }
public IHttpActionResult PutUserComment(int id, UserComment userComment) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != userComment.UserCommentId) { return BadRequest(); } db.Entry(userComment).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!UserCommentExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }