public IHttpActionResult Post(UserRatingCreate rating) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateUserRatingService(); if (!service.CreateUserRating(rating)) { return(InternalServerError()); } return(Ok()); }
public int CreateRating(UserRatingCreate model) { var exchange = _context.Exchanges.Single(e => e.Id == model.ExchangeId); //If Reciever Id is null the exchange has nto been completed if (exchange.ReceiverId is null) { return(2); } //Only the reciever should be able to rate exchange if (_userId != exchange.ReceiverId) { return(3); } //Every exchange Should Only Have One Rating if (_context.UserRatings.Where(r => r.ExchangeId == model.ExchangeId).Count() >= 1) { return(4); } UserRating entity = new UserRating { UserId = _context.Exchanges.Single(e => e.Id == model.ExchangeId).SenderId, ExchangeId = model.ExchangeId, ExchangeRating = model.ExchangeRating }; _context.UserRatings.Add(entity); if (_context.SaveChanges() == 1) { return(0); } else { return(1); } }
public IHttpActionResult PostRating(UserRatingCreate rating) { if (!ModelState.IsValid) return BadRequest(ModelState); var service = CreateRatingService(); switch (service.CreateRating(rating)) { case 0: return Ok("Rating Added"); case 1: return InternalServerError(); case 2: return BadRequest("Exchange has no RecieverId"); case 3: return BadRequest("Only the reciever can rate exchanges"); case 4: return BadRequest("Exchange has already been rated, if you would like to modify rating please use update method"); default: return InternalServerError(); } }