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()
                })));
            }
        }