Ejemplo n.º 1
0
        public async Task <IActionResult> AddPromotion([FromBody] AddPromotionRequest promo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Restaurant restaurant;

            try {
                var user = await _userManager.GetUserAsync(User);

                restaurant = await _context.Restaurant.SingleOrDefaultAsync(m => m.Id == user.RestaurantId);
            }
            catch (Exception) {
                return(Ok(new StandardStatusResponse(false, "Problem z Twoim kontem")));
            }

            var newPromotion = new Promotion
            {
                Restaurant   = restaurant,
                RestaurantId = restaurant.Id,
                Description  = promo.Description
            };

            string[] ses;
            try {
                ses = promo.Tags.Split(',');
                for (var i = 0; i < ses.Length; i++)
                {
                    ses[i] = ses[i].Trim();
                }
                if (ses.Any(s => s == ""))
                {
                    throw new Exception();
                }
            }
            catch (Exception) {
                return(Ok(new StandardStatusResponse(false, "Nieprawidlowe wartości w tagach")));
            }

            newPromotion.Tags = "";
            foreach (var s in ses)
            {
                newPromotion.Tags += s + ",";
            }
            newPromotion.Tags           = newPromotion.Tags.Remove(newPromotion.Tags.Length - 1);
            newPromotion.RepetitionMode = promo.RepetitionMode;
            try {
                string[] strings;
                switch (newPromotion.RepetitionMode)
                {
                case Enums.PeriodEnum.NoLimit:
                    newPromotion.DateStart = null;
                    newPromotion.DateEnd   = null;
                    break;

                case Enums.PeriodEnum.Once:
                    if (string.IsNullOrEmpty(promo.StartTime) ||
                        string.IsNullOrEmpty(promo.DateRange) || string.IsNullOrEmpty(promo.EndTime))
                    {
                        return(Ok(new StandardStatusResponse(false, "Nie podano daty lub godzin")));
                    }
                    var split = promo.DateRange.Split(' ');
                    newPromotion.DateStart = DateTime.Parse(split[0] + " " + promo.StartTime);
                    newPromotion.DateEnd   = DateTime.Parse(split[3] + " " + promo.EndTime);
                    if (DateTime.Compare(newPromotion.DateStart.Value, newPromotion.DateEnd.Value) >= 0)
                    {
                        return(Ok(new StandardStatusResponse(false,
                                                             "Promocja kończy się wcześniej niż się rozpoczyna lub trwa za krótko")));
                    }
                    break;

                case Enums.PeriodEnum.Daily:
                    strings = promo.StartTime.Split(':');
                    newPromotion.DateStart = new DateTime(2000, 1, 1, int.Parse(strings[0]), int.Parse(strings[1]), 0);
                    strings = promo.EndTime.Split(':');
                    newPromotion.DateEnd = new DateTime(2000, 1, 1, int.Parse(strings[0]), int.Parse(strings[1]), 0);
                    if (DateTime.Compare(newPromotion.DateStart.Value, newPromotion.DateEnd.Value) >= 0)
                    {
                        return(Ok(new StandardStatusResponse(false,
                                                             "Godzina rozpoczęcia jest wcześniejsza niż zakończenia")));
                    }
                    break;

                case Enums.PeriodEnum.Weekly:
                    strings = promo.StartTime.Split(':');
                    newPromotion.DateStart = new DateTime(2000, 1, 1, int.Parse(strings[0]), int.Parse(strings[1]), 0);
                    strings = promo.EndTime.Split(':');
                    newPromotion.DateEnd = new DateTime(2000, 1, 1, int.Parse(strings[0]), int.Parse(strings[1]), 0);
                    if (DateTime.Compare(newPromotion.DateStart.Value, newPromotion.DateEnd.Value) >= 0)
                    {
                        return(Ok(new StandardStatusResponse(false,
                                                             "Godzina rozpoczęcia jest wcześniejsza niż zakończenia")));
                    }
                    if (promo.DaysInWeek.Count != 1)
                    {
                        return(Ok(new StandardStatusResponse(false, "Nieprawidłowy dzień powtarzania")));
                    }
                    newPromotion.AddDaysOfWeek(promo.DaysInWeek);
                    break;

                case Enums.PeriodEnum.SingleDays:
                    newPromotion.AddDaysOfWeek(promo.DaysInWeek);
                    newPromotion.DateStart = newPromotion.DateEnd = null;
                    newPromotion.AddDaysOfWeek(promo.DaysInWeek);
                    break;

                default:
                    return(Ok(new StandardStatusResponse(false, "Wybrano zły sposób powtarzania")));
                }
            }
            catch (Exception ex) {
                return(Ok(new StandardStatusResponse(false,
                                                     ex.Message)));
            }

            _context.Promotions.Add(newPromotion);
            try {
                await _context.SaveChangesAsync();

                return(Ok(new StandardStatusResponse(true, "Wszystko ok")));
            }
            catch (Exception ex) {
                return(Ok(new StandardStatusResponse(false, ex.InnerException.Message)));
            }
        }