private Boolean MapRatingToModel(UserPartyRating partyRating, ref UserParty userParty) { Boolean hasChanged = false; //Value mapping if (userParty.GeneralRating != partyRating.GeneralRating) { userParty.GeneralRating = partyRating.GeneralRating; hasChanged = true; } if (userParty.PriceRating != partyRating.PriceRating) { userParty.PriceRating = partyRating.PriceRating; hasChanged = true; } if (userParty.LocationRating != partyRating.LocationRating) { userParty.LocationRating = partyRating.LocationRating; hasChanged = true; } if (userParty.MoodRating != partyRating.MoodRating) { userParty.MoodRating = partyRating.MoodRating; hasChanged = true; } return(hasChanged); }
private String CheckRatingValuesRange(UserPartyRating partyRating) { //Check rating values if (!CheckRatingValue(partyRating.GeneralRating)) { return("GeneralRating value is not beetween -1 and 1."); } else if (!CheckRatingValue(partyRating.PriceRating)) { return("PriceRating value is not beetween -1 and 1."); } else if (!CheckRatingValue(partyRating.LocationRating)) { return("LocationRating value is not beetween -1 and 1."); } else if (!CheckRatingValue(partyRating.MoodRating)) { return("MoodRating value is not beetween -1 and 1."); } else { return(String.Empty); } }
public ActionResult SetRating(Guid?id, [FromBody] UserPartyRating partyRating) { try { //Check if PartyId is valid if (!Validator.IsGuidValid(id.ToString())) { return(BadRequest("Party ID is not valid.")); } else { //Select party var party = _dbContext.PartyItems .FirstOrDefault(p => p.PartyId == id); if (party == null) { return(NotFound("Party not found.")); } else if (party.PartyDate.Date != DateTime.Today.Date && party.PartyDate.AddDays(1).Date != DateTime.Today.Date) { return(BadRequest("It's just possible to vote on the day of the party or the day after.")); } else if (!TryValidateModel(partyRating)) { return(BadRequest(new UserPartyRating())); } else { UserParty userParty = _dbContext.UserPartyItems .FirstOrDefault(up => up.PartyId == party.PartyId && up.UserId == User.UserId); if (userParty == null) { return(BadRequest("Can't rate a party without commitment.")); } else if (userParty.EventCommitment == EventCommitmentState.Rejected) { return(BadRequest("Can't rate the party. Users current commitment is rejected.")); } else { String messageRatingRange = CheckRatingValuesRange(partyRating); if (!String.IsNullOrEmpty(messageRatingRange)) { //Something is wrong with the range of the rating values return(BadRequest(messageRatingRange)); } else { Boolean hasChanged = MapRatingToModel(partyRating, ref userParty); if (!hasChanged) { //Nothing changed return(Ok()); } else { _dbContext.UserPartyItems.Update(userParty); _dbContext.SaveChanges(); return(Ok()); } } } } } } catch (Exception) { return(BadRequest()); } }