public HttpResponseMessage ResetPassword(ConfirmRequest resetRequest) { try { var user = !string.IsNullOrEmpty(resetRequest.UserId) ? _context.Users.SingleOrDefault(x => x.Id == resetRequest.UserId) : _context.Users.SingleOrDefault(x => x.Email == resetRequest.Email); if (user == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_USER_NOTFOUND, new List <string> { resetRequest.UserId ?? resetRequest.Email }))); } var confirmCode = AuthUtils.RandomNumString(8); user.SaltedAndHashedCode = AuthUtils.Hash(confirmCode, user.Salt); user.ResetRequested = true; _context.SaveChanges(); var profile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == user.Id); using (MailSender mailSender = new MailSender()) { var bem = new BaseEmailMessage { Code = ConstVals.Restore, ToUserId = user.Id, ToUserName = profile.FirstName, ToUserEmail = user.Email, ConfirmCode = confirmCode }; mailSender.Create(_context, bem); } return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_USER_RESET_REQUESTED, new List <string> { user.Id }))); } catch (Exception ex) { Debug.WriteLine(ex); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_EXCEPTION, new List <string> { ex.ToString() }))); } }
public HttpResponseMessage SetFavorite(string cardId) { try { var respList = new List <string>(); // Check advertId is not NULL if (cardId == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_FAVORITE_CARDID_NULL))); } // Check Current User var currentUser = User as ServiceUser; if (currentUser == null) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_UNAUTH))); } var account = AuthUtils.GetUserAccount(_context, currentUser); if (account == null) { respList.Add(currentUser.Id); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_NOTFOUND, respList))); } var currentCard = _context.Cards.AsNoTracking().SingleOrDefault(a => a.Id == cardId); if (currentCard == null) { respList.Add(cardId); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_CARD_NOTFOUND, respList))); } if (currentCard.UserId == account.UserId) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.CreateBool(RespH.SRV_FAVORITE_WRONG_USER))); } bool status; var favorite = _context.Favorites.SingleOrDefault(f => f.CardId == cardId && f.UserId == account.UserId); if (favorite == null) { var favoriteGUID = SequentialGuid.NewGuid().ToString(); _context.Favorites.Add(new Favorite { Id = favoriteGUID, CardId = cardId, UserId = account.UserId }); _context.SaveChanges(); // Create Notification Notifications.Create(_context, currentCard.UserId, ConstVals.General, RespH.SRV_NOTIF_CARD_FAVORITED, favoriteGUID, null, null); var user = _context.Users.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId); var profile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId); if (user.EmailNotifications) { using (MailSender mailSender = new MailSender()) { var bem = new BaseEmailMessage { Code = RespH.SRV_NOTIF_CARD_FAVORITED, CardId = currentCard.Id, FromUserName = profile.FirstName, FromUserEmail = user.Email, ToUserName = currentCard.User.Profile.FirstName, ToUserEmail = currentCard.User.Email, UnsubscrCode = currentCard.User.EmailSubCode }; mailSender.Create(_context, bem); } } status = true; } else { var notif = _context.Notifications.SingleOrDefault(n => n.FavoriteId == favorite.Id); if (notif != null) { _context.Notifications.Remove(notif); } _context.SaveChanges(); _context.Favorites.Remove(favorite); status = false; } _context.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, RespH.CreateBool(RespH.SRV_DONE, new List <bool> { status }))); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_EXCEPTION, new List <string> { ex.InnerException.ToString() }))); } }
public HttpResponseMessage PostFeedback(FeedbackDTO feedback) { try { var respList = new List <string>(); if (feedback == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_FEEDBACK_NULL))); } if (feedback.Text == null) { respList.Add("Text"); } if (feedback.Type == null) { respList.Add("Type"); } if (feedback.UserId != null) { var user = _context.Users.AsNoTracking().SingleOrDefault(x => x.Id == feedback.UserId); var profile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == feedback.UserId); if (user == null || profile == null) { respList.Add(feedback.UserId); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_NOTFOUND, respList))); } feedback.UserName = profile.FirstName + " " + profile.LastName; feedback.Email = user.Email; } else { if (string.IsNullOrWhiteSpace(feedback.UserName)) { respList.Add("Username"); } } if (feedback.AnswerByEmail && feedback.Email == null) { respList.Add("Email"); } if (respList.Count > 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_FEEDBACK_REQUIRED, respList))); } var bem = new BaseEmailMessage(); if (feedback.Type == ConstVals.Abuse) { var abuser = _context.Users.SingleOrDefault(x => x.Id == feedback.AbuserId); var abuserProfile = _context.Profile.SingleOrDefault(x => x.Id == feedback.AbuserId); if (abuser == null || abuserProfile == null) { respList.Add(feedback.AbuserId); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_FEEDBACK_ABUSER_NOTFOUND, respList))); } StringBuilder addText = new StringBuilder(); addText.Append("Жалоба на пользователя: <br>"); addText.Append(abuserProfile.FirstName + " " + abuserProfile.LastName + "<br>"); addText.Append("Email: " + abuser.Email + "<br>"); addText.Append("Id: " + abuser.Id + "<br>"); addText.Append("Текст жалобы: <br>"); addText.Append(feedback.Text); feedback.Text = addText.ToString(); bem.Code = ConstVals.Abuse; } else { bem.Code = ConstVals.Feedback; } var feedbackGuid = SequentialGuid.NewGuid().ToString(); _context.Feedbacks.Add( new Feedback { Id = feedbackGuid, AbuserId = feedback.AbuserId, UserId = feedback.UserId, UserName = feedback.UserName, Type = feedback.Type, Email = feedback.Email, Text = feedback.Text, AnswerByEmail = feedback.AnswerByEmail }); _context.SaveChanges(); using (MailSender mailSender = new MailSender()) { bem.ToUserEmail = Environment.GetEnvironmentVariable(feedback.Type == ConstVals.Abuse ? "ABUSE_EMAIL" : "FEEDBACK_EMAIL"); bem.ToUserName = "******"; bem.FromUserEmail = feedback.Email; bem.FromUserName = feedback.UserName; bem.Text = feedback.Text; bem.AnswerByEmail = feedback.Type == ConstVals.Abuse || feedback.AnswerByEmail; mailSender.Create(_context, bem); } respList.Add(feedbackGuid); return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_CREATED, respList))); } catch (Exception ex) { Debug.WriteLine(ex); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_EXCEPTION, new List <string> { ex.ToString() }))); } }
public HttpResponseMessage Post(RegistrationRequest registrationRequest) { try { var respList = new List <string>(); if (!AuthUtils.IsEmailValid(registrationRequest.Email)) { respList.Add(registrationRequest.Email); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_REG_INVALID_EMAIL, respList))); } if (registrationRequest.Password.Length < 8) { respList.Add(registrationRequest.Password); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_REG_INVALID_PASSWORD, respList))); } var user = _context.Users.AsNoTracking().SingleOrDefault(a => a.Email == registrationRequest.Email); if (user != null) { respList.Add(registrationRequest.Email); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_REG_EXISTS_EMAIL, respList))); } var salt = AuthUtils.GenerateSalt(); var confirmCode = AuthUtils.RandomNumString(6); var userId = SequentialGuid.NewGuid().ToString(); var newUser = new User { Id = userId, Email = registrationRequest.Email, Salt = salt, EmailSubCode = SequentialGuid.NewGuid().ToString(), SaltedAndHashedPassword = AuthUtils.Hash(registrationRequest.Password, salt), SaltedAndHashedEmail = AuthUtils.Hash(confirmCode, salt) }; _context.Users.Add(newUser); _context.SaveChanges(); AuthUtils.CreateAccount(_context, StandartLoginProvider.ProviderName, registrationRequest.Email, StandartLoginProvider.ProviderName + ":" + registrationRequest.Email, registrationRequest.Email, registrationRequest.FirstName); using (MailSender mailSender = new MailSender()) { var bem = new BaseEmailMessage { Code = ConstVals.Reg, ToUserId = newUser.Id, ToUserEmail = registrationRequest.Email, ToUserName = registrationRequest.FirstName, ConfirmCode = confirmCode }; mailSender.Create(_context, bem); } respList.Add(newUser.Id); return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_CREATED, respList))); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_EXCEPTION, new List <string> { ex.ToString() }))); } }
public HttpResponseMessage AcceptDeclineReservation(string reservId, string status) { try { var respList = new List <string>(); // Check status is not NULL if (status == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_NULL))); } // Check Status if (status != ConstVals.Accepted && status != ConstVals.Declined) { respList.Add(status); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_WRONG_STATUS, respList))); } // Check Current User var currentUser = User as ServiceUser; if (currentUser == null) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_UNAUTH))); } var account = AuthUtils.GetUserAccount(_context, currentUser); if (account == null) { respList.Add(currentUser.Id); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_NOTFOUND, respList))); } ResponseDTO resp = CheckHelper.IsProfileFill(_context, account.UserId, true); if (resp != null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, resp)); } //Check Reservation var currentReservation = _context.Reservations.SingleOrDefault(r => r.Id == reservId); // Check Reservation is not NULL if (currentReservation == null) { respList.Add(reservId); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_NOTFOUND, respList))); } var card = _context.Cards.Include("Apartment") .AsNoTracking() .SingleOrDefault(a => a.Id == currentReservation.CardId); // Check CARD is not NULL if (card == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_CARD_NULL))); } // Check CARD User if (card.UserId != account.UserId) { respList.Add(card.UserId); respList.Add(account.UserId); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_CARD_WRONG_USER, respList))); } string notifCode; //Check status if (status == ConstVals.Accepted) { // Check Available Dates var reservationDates = new TimeRange(currentReservation.DateFrom, currentReservation.DateTo); var unavailableDates = new List <TimeRange>(); var cardDates = _context.Dates.Where(x => x.CardId == card.Id); if (cardDates.Any()) { foreach (var unDate in cardDates) { unavailableDates.Add(new TimeRange(unDate.DateFrom, unDate.DateTo)); } if (unavailableDates.Any(unavailableDate => unavailableDate.IntersectsWith(reservationDates))) { respList.Add(reservationDates.ToString()); respList.Add(unavailableDates.ToString()); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_UNAVAILABLE_DATE, respList))); } } var currentReservations = _context.Reservations.AsNoTracking().Where( r => r.CardId == currentReservation.CardId && currentReservation.Status == ConstVals.Accepted); foreach (var currentReserv in currentReservations) { var reservedDates = new TimeRange(currentReserv.DateFrom, currentReserv.DateTo); if (reservedDates.IntersectsWith(reservationDates)) { respList.Add(reservationDates.ToString()); respList.Add(reservedDates.ToString()); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_UNAVAILABLE_DATE, respList))); } } notifCode = RespH.SRV_NOTIF_RESERV_ACCEPTED; } else { notifCode = RespH.SRV_NOTIF_RESERV_DECLINED; } currentReservation.Status = status; _context.MarkAsModified(currentReservation); _context.SaveChanges(); // Create Notification Notifications.Create(_context, currentReservation.UserId, ConstVals.General, notifCode, null, currentReservation.Id, null); var fromUser = _context.Users.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId); var fromProfile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId); var toUser = _context.Users.AsNoTracking().SingleOrDefault(x => x.Id == currentReservation.UserId); var toProfile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == currentReservation.UserId); using (MailSender mailSender = new MailSender()) { var bem = new BaseEmailMessage { Code = notifCode, CardName = card.Name, CardType = DicHelper.GetCardTypeByLang(card.Apartment.Type), CardDescription = card.Description, CardId = card.Id, DateFrom = currentReservation.DateFrom, DateTo = currentReservation.DateTo, FromUserName = fromProfile.FirstName, FromUserEmail = fromUser.Email, ToUserName = toProfile.FirstName, ToUserEmail = toUser.Email, UnsubscrCode = toUser.EmailSubCode }; mailSender.Create(_context, bem); } respList.Add(reservId); return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_UPDATED, respList))); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_EXCEPTION, new List <string> { ex.InnerException.ToString() }))); } }
public HttpResponseMessage MakeReservation(string cardId, string gender, DateTime dateFrom, DateTime dateTo) { try { var respList = new List <string>(); // Check Reservation is not NULL if (cardId == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_NULL))); } var card = _context.Cards.Include("Apartment").SingleOrDefault(a => a.Id == cardId); // Check CARD is not NULL if (card == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_CARD_NULL))); } // Check Current User var currentUser = User as ServiceUser; if (currentUser == null) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_UNAUTH))); } var account = AuthUtils.GetUserAccount(_context, currentUser); if (account == null) { respList.Add(currentUser.Id); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_NOTFOUND, respList))); } // Check Card Owner if (card.UserId == account.UserId) { respList.Add(account.UserId); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_SELF, respList))); } ResponseDTO resp = CheckHelper.IsProfileFill(_context, account.UserId); if (resp != null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, resp)); } // Check CARD User //if (CARD.UserId != account.UserId) //{ // respList.Add(CARD.UserId); // respList.Add(account.UserId); // return this.Request.CreateResponse(HttpStatusCode.BadRequest, // RespH.Create(RespH.SRV_CARD_WRONG_USER, respList)); //} // Check Dates if (DateTime.Compare(dateFrom, dateTo) > 0) { respList.Add(dateFrom.ToLocalTime().ToString(CultureInfo.InvariantCulture)); respList.Add(dateTo.ToLocalTime().ToString(CultureInfo.InvariantCulture)); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_CARD_WRONG_DATE, respList))); } // Check Reservation already exists if ( _context.Reservations.AsNoTracking().Any( x => x.UserId == account.UserId && x.CardId == cardId && x.DateFrom == dateFrom && x.DateTo == dateTo)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_EXISTS))); } // Check Available Dates var reservationDates = new TimeRange(dateFrom, dateTo); var unavailableDates = _context.Dates.AsNoTracking().Where(x => x.CardId == card.Id) .ToList() .Select(unDate => new TimeRange(unDate.DateFrom, unDate.DateTo)) .ToList(); if (unavailableDates.Any(unavailableDate => unavailableDate.IntersectsWith(reservationDates))) { respList.Add(reservationDates.ToString()); respList.Add(unavailableDates.ToString()); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_UNAVAILABLE_DATE, respList))); } var currentReservations = _context.Reservations.AsNoTracking() .Where(r => r.CardId == cardId && r.Status == ConstVals.Accepted); foreach (var currentReservation in currentReservations) { var reservedDates = new TimeRange(currentReservation.DateFrom, currentReservation.DateTo); if (reservedDates.IntersectsWith(reservationDates)) { respList.Add(reservationDates.ToString()); respList.Add(reservedDates.ToString()); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_UNAVAILABLE_DATE, respList))); } } var reservationGuid = SequentialGuid.NewGuid().ToString(); _context.Reservations.Add(new Reservation { Id = reservationGuid, CardId = cardId, UserId = account.UserId, Status = ConstVals.Pending, Gender = gender, DateFrom = dateFrom, DateTo = dateTo }); _context.SaveChanges(); // Create Notification Notifications.Create(_context, card.UserId, ConstVals.General, RespH.SRV_NOTIF_RESERV_PENDING, null, reservationGuid, null); var user = _context.Users.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId); var profile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId); if (user.EmailNotifications) { using (MailSender mailSender = new MailSender()) { var bem = new BaseEmailMessage { Code = RespH.SRV_NOTIF_RESERV_PENDING, CardName = card.Name, CardType = DicHelper.GetCardTypeByLang(card.Apartment.Type), CardDescription = card.Description, DateFrom = dateFrom, DateTo = dateTo, ToUserName = profile.FirstName, ToUserEmail = user.Email, CardId = card.Id, UnsubscrCode = user.EmailSubCode }; mailSender.Create(_context, bem); } } var cardUser = _context.Users.AsNoTracking().SingleOrDefault(x => x.Id == card.UserId); var cardProfile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == card.UserId); if (cardUser.EmailNotifications) { using (MailSender mailSender = new MailSender()) { var bem = new BaseEmailMessage { Code = RespH.SRV_NOTIF_RESERV_NEW, CardName = card.Name, CardType = DicHelper.GetCardTypeByLang(card.Apartment.Type), CardDescription = card.Description, FromUserName = profile.FirstName, DateFrom = dateFrom, DateTo = dateTo, ToUserName = cardProfile.FirstName, ToUserEmail = cardUser.Email, CardId = card.Id, UnsubscrCode = cardUser.EmailSubCode }; mailSender.Create(_context, bem); } } respList.Add(reservationGuid); return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_CREATED, respList))); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_EXCEPTION, new List <string> { ex.InnerException.ToString() }))); } }
public HttpResponseMessage SetEmail(UserDTO userdata) { try { var respList = new List <string>(); if (string.IsNullOrWhiteSpace(userdata.Email)) { respList.Add("Email"); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_USER_REQUIRED, respList))); } if (!AuthUtils.IsEmailValid(userdata.Email)) { respList.Add(userdata.Email); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_REG_INVALID_EMAIL, respList))); } var usersame = _context.Users.AsNoTracking().SingleOrDefault(a => a.Email == userdata.Email); if (usersame != null) { respList.Add(userdata.Email); return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_REG_EXISTS_EMAIL, respList))); } // Check Current User var currentUser = User as ServiceUser; if (currentUser == null) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_UNAUTH))); } var account = AuthUtils.GetUserAccount(_context, currentUser); if (account == null) { respList.Add(currentUser.Id); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_NOTFOUND, respList))); } var user = _context.Users.SingleOrDefault(x => x.Id == account.UserId); var profile = _context.Profile.SingleOrDefault(x => x.Id == account.UserId); if (user == null || profile == null) { respList.Add(account.UserId); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_NOTFOUND, respList))); } if (!string.IsNullOrWhiteSpace(user.Email)) { respList.Add(user.Email); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_EXISTS, respList))); } var salt = AuthUtils.GenerateSalt(); var confirmCode = AuthUtils.RandomNumString(6); user.Email = userdata.Email; user.Salt = salt; user.SaltedAndHashedEmail = AuthUtils.Hash(confirmCode, salt); _context.MarkAsModified(user); _context.SaveChanges(); using (MailSender mailSender = new MailSender()) { var bem = new BaseEmailMessage { Code = ConstVals.Reg, ToUserId = user.Id, ToUserEmail = user.Email, ToUserName = profile.FirstName, ConfirmCode = confirmCode }; mailSender.Create(_context, bem); } respList.Add(user.Id); return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_UPDATED, respList))); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_EXCEPTION, new List <string> { ex.ToString() }))); } }
public HttpResponseMessage PostReview(string resId, ReviewDTO review) { try { var respList = new List <string>(); ResponseDTO resp; // Check Card is not NULL if (review == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_REVIEW_NULL))); } // Check Reservation is not NULL var reservation = _context.Reservations.AsNoTracking().SingleOrDefault(x => x.Id == resId); if (reservation == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_RESERVATION_NOTFOUND))); } // Check Reservation Status is Accepted if (reservation.Status != ConstVals.Accepted) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_REVIEW_WRONG_RESERV_STATUS))); } // Check Reservation Dates if (reservation.DateTo >= DateTime.Now) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_REVIEW_WRONG_DATE))); } // Check Review Text is not NULL resp = CheckHelper.IsNull(review.Text, "Text", RespH.SRV_REVIEW_REQUIRED); if (resp != null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, resp)); } // Check Review Rating is not NULL //resp = CheckHelper.IsNull(review.Rating, "Rating", RespH.SRV_REVIEW_REQUIRED); //if (resp != null) return this.Request.CreateResponse(HttpStatusCode.BadRequest, resp); // Check Current User var currentUser = User as ServiceUser; if (currentUser == null) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_UNAUTH))); } var account = AuthUtils.GetUserAccount(_context, currentUser); if (account == null) { respList.Add(currentUser.Id); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_NOTFOUND, respList))); } resp = CheckHelper.IsProfileFill(_context, account.UserId); if (resp != null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, resp)); } var newReview = new Review(); // Set FromUserId newReview.FromUserId = account.UserId; // Set ToUserId if (reservation.UserId == account.UserId) { newReview.ToUserId = reservation.Card.UserId; } if (reservation.Card.UserId == account.UserId) { newReview.ToUserId = reservation.UserId; } // Check Review doesn't already exist var currentReview = _context.Reviews.AsNoTracking().SingleOrDefault( r => r.ReservationId == reservation.Id && r.FromUserId == newReview.FromUserId && r.ToUserId == newReview.ToUserId); if (currentReview != null) { respList.Add(currentReview.Id); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_REVIEW_EXISTS, respList))); } var reviewGuid = SequentialGuid.NewGuid().ToString(); newReview.Id = reviewGuid; newReview.ReservationId = reservation.Id; newReview.Text = review.Text; _context.Reviews.Add(newReview); string notifCode; // Rating Calculation if (review.Rating > 0) { var profile = _context.Profile.SingleOrDefault(x => x.Id == newReview.ToUserId); if (profile == null) { respList.Add(newReview.ToUserId); return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_USER_NOTFOUND, respList))); } newReview.Rating = review.Rating; notifCode = RespH.SRV_NOTIF_REVIEW_RATING_ADDED; profile.RatingCount += 1; profile.Score += newReview.Rating; profile.Rating = profile.Score / profile.RatingCount; } else { newReview.Rating = 0; notifCode = RespH.SRV_NOTIF_REVIEW_ADDED; } _context.SaveChanges(); // Create Notification Notifications.Create(_context, newReview.ToUserId, ConstVals.General, notifCode, null, null, reviewGuid); var fromUser = _context.Users.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId); var fromProfile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId); var toUser = _context.Users.AsNoTracking().SingleOrDefault(x => x.Id == newReview.ToUserId); var toProfile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == newReview.ToUserId); if (toUser.EmailNewsletter) { using (MailSender mailSender = new MailSender()) { var bem = new BaseEmailMessage { Code = notifCode, FromUserName = fromProfile.FirstName, FromUserEmail = fromUser.Email, ToUserName = toProfile.FirstName, ToUserEmail = toUser.Email, ReviewText = newReview.Text, ReviewRating = newReview.Rating, UnsubscrCode = toUser.EmailSubCode }; mailSender.Create(_context, bem); } } respList.Add(reviewGuid); return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_CREATED, respList))); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_EXCEPTION, new List <string> { ex.InnerException.ToString() }))); } }