public HttpResponseMessage ClearAll()
        {
            try
            {
                var respList = new List <string>();

                // 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 notifications = _context.Notifications.Where(x => x.UserId == account.UserId && x.Readed == false);

                foreach (var notification in notifications)
                {
                    notification.Readed = true;
                    _context.MarkAsModified(notification);
                }

                _context.SaveChanges();
                return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_UPDATED)));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                              RespH.Create(RespH.SRV_EXCEPTION, new List <string> {
                    ex.InnerException.ToString()
                })));
            }
        }
Пример #2
0
        public HttpResponseMessage Unsubscribe(UnsubscribeRequest req)
        {
            try
            {
                var respList = new List <string>();
                var user     = _context.Users.SingleOrDefault(u => u.EmailSubCode == req.Code);
                if (user == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_USER_NOTFOUND)));
                }
                switch (req.Type)
                {
                case "newsletter":
                    user.EmailNewsletter = false;
                    break;

                case "notifications":
                    user.EmailNotifications = false;
                    break;

                default:
                    user.EmailNewsletter    = false;
                    user.EmailNotifications = false;
                    break;
                }
                _context.MarkAsModified(user);
                _context.SaveChanges();
                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 SetDefault(string id)
        {
            try
            {
                var respList = new List <string>();
                var pic      = _context.Pictures.SingleOrDefault(x => x.Id == id);
                if (pic == null)
                {
                    respList.Add(id);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_PICTURE_NOTFOUND, 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 pics =
                    _context.Pictures.Where(
                        x => x.Apartments.Any(a => a.UserId == account.UserId && a.Pictures.Any(p => p.Id == id)));
                if (pics.Any())
                {
                    foreach (var other in pics.Where(x => x.Id != id && x.Default))
                    {
                        other.Default = false;
                    }
                    pic.Default = true;
                }
                else
                {
                    if (_context.Profile.Any(x => x.Picture.Id == id && x.Id == account.UserId))
                    {
                        pic.Default = true;
                    }
                }
                if (pic.Default)
                {
                    _context.MarkAsModified(pic);
                    _context.SaveChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_UPDATED)));
                }

                return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_USER_WRONG_USER)));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                              RespH.Create(RespH.SRV_EXCEPTION, new List <string> {
                    ex.ToString()
                })));
            }
        }
        public HttpResponseMessage PutApartment(string id, ApartmentDTO apartment)
        {
            try
            {
                var         respList = new List <string>();
                ResponseDTO resp;
                // Check Apartment is not NULL
                if (apartment == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_APARTMENT_NULL)));
                }

                // Check Current Apartment is not NULL
                var apartmentCurrent = _context.Apartments.SingleOrDefault(a => a.Id == id);
                if (apartmentCurrent == null)
                {
                    respList.Add(id);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_APARTMENT_NOTFOUND, respList)));
                }

                // Check Apartment Name is not NULL
                resp = CheckHelper.IsNull(apartment.Name, "Name", RespH.SRV_APARTMENT_REQUIRED);
                if (resp != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, resp));
                }

                // Check Apartment Adress is not NULL
                resp = CheckHelper.IsNull(apartment.Adress, "Adress", RespH.SRV_APARTMENT_REQUIRED);
                if (resp != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, resp));
                }


                // Check Apartment Type is not NULL
                resp = CheckHelper.IsNull(apartment.Type, "Type", RespH.SRV_APARTMENT_REQUIRED);
                if (resp != null)
                {
                    return(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)));
                }

                // Check Apartment User
                if (apartment.UserId != account.UserId)
                {
                    respList.Add(apartment.UserId);
                    respList.Add(account.UserId);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_APARTMENT_WRONG_USER, respList)));
                }
                if (apartmentCurrent.UserId != account.UserId)
                {
                    respList.Add(apartmentCurrent.UserId);
                    respList.Add(account.UserId);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_APARTMENT_WRONG_USER, respList)));
                }

                // Update Apartment
                apartmentCurrent.Name      = apartment.Name;
                apartmentCurrent.Adress    = apartment.Adress;
                apartmentCurrent.Type      = apartment.Type;
                apartmentCurrent.Options   = apartment.Options;
                apartmentCurrent.Latitude  = apartment.Latitude;
                apartmentCurrent.Longitude = apartment.Longitude;
                apartmentCurrent.Lang      = apartment.Lang;
                _context.MarkAsModified(apartmentCurrent);
                _context.SaveChanges();

                respList.Add(apartment.Id);
                return(Request.CreateResponse(HttpStatusCode.OK, RespH.Create(RespH.SRV_UPDATED, respList)));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.InnerException);
                return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                              RespH.Create(RespH.SRV_EXCEPTION, new List <string> {
                    ex.InnerException.ToString()
                })));
            }
        }
Пример #5
0
        public HttpResponseMessage ReqeustCode()
        {
            try
            {
                var         respList = new List <string>();
                ResponseDTO 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)));
                }

                var user = _context.Users.SingleOrDefault(x => x.Id == account.UserId);
                if (user == null)
                {
                    respList.Add(account.UserId);
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized,
                                                  RespH.Create(RespH.SRV_USER_NOTFOUND, respList)));
                }
                var profile = _context.Profile.AsNoTracking().SingleOrDefault(x => x.Id == account.UserId);


                resp = CheckHelper.IsNull(profile.Phone, "Phone", RespH.SRV_USER_REQUIRED);
                if (resp != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, resp));
                }

                if (user.PhoneStatus == ConstVals.PConf)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_USER_PHONE_CONFIRMED, new List <string> {
                        user.PhoneStatus
                    })));
                }
                if (user.PhoneCodeRequestedAt.HasValue && user.PhoneStatus == ConstVals.PPending)
                {
                    if (user.PhoneCodeRequestedAt.Value.AddMinutes(3) > DateTime.Now)
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                      RespH.Create(RespH.SRV_USER_PHONE_CONFIRM_REQUESTED,
                                                                   new List <string> {
                            user.PhoneCodeRequestedAt.ToString()
                        })));
                    }
                }
                var confirmCode = AuthUtils.RandomNumString(4);
                user.SaltedAndHashedSmsCode = AuthUtils.Hash(confirmCode, user.Salt);
                user.PhoneCodeRequestedAt   = DateTime.Now;
                user.PhoneStatus            = ConstVals.PPending;
                _context.MarkAsModified(user);
                _context.SaveChanges();

                var    regArt = _context.Article.SingleOrDefault(x => x.Name == ConstVals.Reg && x.Type == ConstVals.Sms);
                string smstext;
                if (regArt != null)
                {
                    smstext = confirmCode + " " + regArt.Text;
                }
                else
                {
                    smstext = confirmCode;
                }
                using (SmsSender sender = new SmsSender())
                {
                    sender.Send(profile.Phone, smstext);
                }
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              RespH.Create(RespH.SRV_DONE, new List <string> {
                    user.PhoneStatus
                })));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                              RespH.Create(RespH.SRV_EXCEPTION, new List <string> {
                    ex.InnerException.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 PutCurrentUser(UserDTO profile)
        {
            try
            {
                var         respList = new List <string>();
                ResponseDTO resp;
                // Check Profile is not NULL
                if (profile == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_USER_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 Profile User
                if (profile.Id != account.UserId)
                {
                    respList.Add(profile.Id);
                    respList.Add(account.UserId);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_USER_WRONG_USER, respList)));
                }

                var user = _context.Users.AsNoTracking().SingleOrDefault(u => u.Id == account.UserId);
                if (user == null)
                {
                    respList.Add(account.UserId);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_USER_NOTFOUND, respList)));
                }
                // Check Current Profile is not NULL
                var profileCurrent = _context.Profile.SingleOrDefault(a => a.Id == account.UserId);
                if (profileCurrent == null)
                {
                    respList.Add(account.UserId);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_USER_NOTFOUND, respList)));
                }

                if (!string.IsNullOrWhiteSpace(profile.Phone))
                {
                    profile.Phone = CheckHelper.CleanPhone(profile.Phone);
                    if (profile.Phone[0] == '7')
                    {
                        if (profile.Phone.Length != 11)
                        {
                            respList.Add(profile.Phone);
                            return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                          RespH.Create(RespH.SRV_PROFILE_WRONG_PHONE, respList)));
                        }
                    }
                    if (profileCurrent.Phone != profile.Phone)
                    {
                        var hasCards = _context.Cards.Any(x => x.UserId == user.Id);
                        if (hasCards)
                        {
                            respList.Add(user.Id);
                            return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                          RespH.Create(RespH.SRV_PROFILE_ERR_UPDATE_PHONE, respList)));
                        }
                        user.PhoneStatus = ConstVals.PUnconf;
                        _context.MarkAsModified(user);
                    }
                }
                else
                {
                    var hasCards = _context.Cards.Any(x => x.UserId == user.Id);
                    if (hasCards)
                    {
                        respList.Add(user.Id);
                        return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                      RespH.Create(RespH.SRV_PROFILE_ERR_UPDATE_PHONE, respList)));
                    }
                    user.PhoneStatus = ConstVals.PUnconf;
                    _context.MarkAsModified(user);
                }
                // Check FirstName is not NULL
                resp = CheckHelper.IsNull(profile.FirstName, "FirstName", RespH.SRV_USER_REQUIRED);
                if (resp != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, resp));
                }

                // Check LastName is not NULL
                //resp = CheckHelper.IsNull(profile.LastName, "LastName", RespH.SRV_USER_REQUIRED);
                //if (resp != null) return Request.CreateResponse(HttpStatusCode.BadRequest, resp);

                // Check Phone is not NULL
                //resp = CheckHelper.IsNull(profile.Phone, "Phone", RespH.SRV_USER_REQUIRED);
                //if (resp != null) return Request.CreateResponse(HttpStatusCode.BadRequest, resp);

                // Check Genderis not NULL
                //resp = CheckHelper.IsNull(profile.Gender, "Gender", RespH.SRV_USER_REQUIRED);
                //if (resp != null) return Request.CreateResponse(HttpStatusCode.BadRequest, resp);

                profileCurrent.FirstName   = profile.FirstName;
                profileCurrent.LastName    = profile.LastName;
                profileCurrent.Gender      = profile.Gender;
                profileCurrent.Birthday    = profile.Birthday;
                profileCurrent.Phone       = profile.Phone;
                profileCurrent.Description = profile.Description;
                user.EmailNotifications    = profile.EmailNotifications;
                user.EmailNewsletter       = profile.EmailNewsletter;
                _context.MarkAsModified(user);
                _context.MarkAsModified(profileCurrent);
                _context.SaveChanges();

                respList.Add(profileCurrent.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 PutArticle(string id, ArticleDTO article)
        {
            try
            {
                // Check Current User
                var currentUser = User as ServiceUser;
                if (currentUser == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_UNAUTH)));
                }
                string[] cred  = currentUser.Id.Split(':');
                string   email = cred[1];
                var      admin = _context.Admins.AsNoTracking().SingleOrDefault(x => x.Email == email);
                if (admin == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized, RespH.Create(RespH.SRV_UNAUTH)));
                }
                var respList = new List <string>();
                if (article == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, RespH.Create(RespH.SRV_ARTICLE_NULL)));
                }

                var articleCurrent = _context.Article.SingleOrDefault(a => a.Id == id);
                if (articleCurrent == null)
                {
                    respList.Add(id);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_ARTICLE_NOTFOUND, respList)));
                }

                if (article.Name == null)
                {
                    respList.Add("Name");
                }
                if (article.Title == null)
                {
                    respList.Add("Title");
                }
                if (article.Text == null)
                {
                    respList.Add("Text");
                }
                if (article.Type == null)
                {
                    respList.Add("Type");
                }
                if (article.Lang == null)
                {
                    respList.Add("Lang");
                }
                if (respList.Count > 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  RespH.Create(RespH.SRV_ARTICLE_REQUIRED, respList)));
                }


                articleCurrent.Name  = article.Name;
                articleCurrent.Title = article.Title;
                articleCurrent.Text  = article.Text;
                articleCurrent.Type  = article.Type;
                articleCurrent.Lang  = article.Lang;

                _context.MarkAsModified(articleCurrent);
                _context.SaveChanges();

                respList.Add(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 PutReview(string revId, ReviewDTO review)
        {
            try
            {
                var         respList = new List <string>();
                ResponseDTO resp;

                // Check Review  exist
                var currentReview =
                    _context.Reviews.SingleOrDefault(
                        r => r.Id == revId);
                if (currentReview == null)
                {
                    respList.Add(revId);
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized,
                                                  RespH.Create(RespH.SRV_REVIEW_NOTFOUND, respList)));
                }

                // Check Reservation is not NULL
                var reservation =
                    _context.Reservations.AsNoTracking().SingleOrDefault(x => x.Id == currentReview.ReservationId);
                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 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));
                }

                // Check Review User
                if (currentReview.FromUserId != account.UserId)
                {
                    respList.Add(currentReview.FromUserId);
                    respList.Add(account.UserId);
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized,
                                                  RespH.Create(RespH.SRV_REVIEW_WRONG_USER, respList)));
                }


                currentReview.Text = review.Text;
                _context.MarkAsModified(currentReview);
                _context.SaveChanges();
                respList.Add(currentReview.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.InnerException.ToString()
                })));
            }
        }