Esempio n. 1
0
 public RateServiceTests()
 {
     this.service = new RateService();
     service.Add(new Rate { RateInformation = "5", }
         );
     service.Add(new Rate { RateInformation = "3" }
         );
 }
Esempio n. 2
0
        public IActionResult RateBook(RateViewModel rateVM)
        {
            if (string.IsNullOrEmpty(rateVM.UserId) && string.IsNullOrEmpty(rateVM.RatedEssenceId) && (rateVM.Value < 1 || rateVM.Value > 5))
            {
                return(RedirectToAction("Error"));
            }
            BookDTO bookTORate = _bookService.Get(rateVM.RatedEssenceId);

            if (bookTORate == null)
            {
                return(RedirectToAction("Error"));
            }
            RateDTO yourRate = new RateDTO {
                BookId = rateVM.RatedEssenceId,
                UserId = rateVM.UserId,
                Value  = rateVM.Value
            };

            List <RateDTO> allRates = _rateService.GetAll().ToList();

            if (allRates != null)
            {
                bool isFinded = false;
                foreach (var r in allRates)
                {
                    if (r.BookId == rateVM.RatedEssenceId && r.UserId == rateVM.UserId)
                    {
                        isFinded         = true;
                        yourRate.Id      = r.Id;
                        bookTORate.Rate += (yourRate.Value - r.Value) / bookTORate.RatesAmount;
                        _rateService.Update(yourRate);
                        _bookService.Update(bookTORate);
                        break;
                    }
                }
                if (!isFinded)
                {
                    uint amount = bookTORate.RatesAmount;
                    bookTORate.RatesAmount++;
                    bookTORate.Rate = (bookTORate.Rate * amount + yourRate.Value) / bookTORate.RatesAmount;
                    _bookService.Update(bookTORate);
                    _rateService.Add(yourRate);
                }
            }
            else
            {
                uint amount = bookTORate.RatesAmount;
                bookTORate.RatesAmount++;
                bookTORate.Rate = (bookTORate.Rate * amount + yourRate.Value) / bookTORate.RatesAmount;
                _bookService.Update(bookTORate);
                _rateService.Add(yourRate);
            }

            return(RedirectToAction("GetBookInfo", "Home", new { id = bookTORate.Id }));
        }
Esempio n. 3
0
 public RateServiceTests()
 {
     this.service = new RateService();
     service.Add(new Rate {
         RateInformation = "5",
     }
                 );
     service.Add(new Rate {
         RateInformation = "3"
     }
                 );
 }
Esempio n. 4
0
        public IActionResult RateBook(string bookId, string userId, decimal rate)
        {
            if (string.IsNullOrEmpty(bookId) || string.IsNullOrEmpty(userId) || rate < 1 || rate > 5)
            {
                RedirectToAction("Error");
            }
            BookDTO bookTORate = _bookService.Get(bookId);

            if (bookTORate == null)
            {
                return(RedirectToAction("Error"));
            }
            RateDTO yourRate = new RateDTO {
                BookId = bookId, UserId = userId, Value = rate
            };
            List <RateDTO> allRates = _rateService.GetAll().ToList();

            if (allRates != null)
            {
                bool isFinded = false;
                foreach (var r in allRates)
                {
                    if (r.BookId == bookId && r.UserId == userId)
                    {
                        isFinded        = true;
                        yourRate.Id     = r.Id;
                        bookTORate.Rate = (bookTORate.Rate * bookTORate.RatesAmount - r.Value + rate) / bookTORate.RatesAmount;
                        _rateService.Update(yourRate);
                        _bookService.Update(bookTORate);
                        break;
                    }
                }
                if (!isFinded)
                {
                    uint amount = bookTORate.RatesAmount;
                    bookTORate.RatesAmount++;
                    bookTORate.Rate = (bookTORate.Rate * amount + rate) / bookTORate.RatesAmount;
                    _bookService.Update(bookTORate);
                    _rateService.Add(yourRate);
                }
            }
            else
            {
                uint amount = bookTORate.RatesAmount;
                bookTORate.RatesAmount++;
                bookTORate.Rate = (bookTORate.Rate * amount + rate) / bookTORate.RatesAmount;
                _bookService.Update(bookTORate);
                _rateService.Add(yourRate);
            }

            return(RedirectToAction("GetBookInfo", "Home", new { id = bookTORate.Id }));
        }
Esempio n. 5
0
        public ActionResult Rate(FormCollection form)
        {
            if (!string.IsNullOrEmpty(form["reportId"]))
            {
                if (Decimal.Parse(form["rating"]) <= 3)
                {
                    if (string.IsNullOrEmpty(form["comment"]))
                    {
                        ModelState.AddModelError("Comment", "The Comment field is required.");
                        return(View());
                    }
                }
                Rate rate = new Rate
                {
                    ReportID = Int16.Parse(form["reportId"]),
                    Value    = Decimal.Parse(form["rating"]),
                    Comment  = form["comment"]
                };

                _rateService.Add(rate);
                _rateService.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
        /// <summary>
        /// Проверка наличия тарифа по умолчнию и изменение, если в файле конфига изменена цена
        /// </summary>
        public void LoadAndCheckDefaultRate()
        {
            var rates = _rateService.GetItem(x => x.IsDefault);

            if (rates == null)
            {
                _rateService.Add(new RateEntity()
                {
                    Price     = System.Convert.ToDecimal(ConfigurationManager.AppSettings["DefaultPrice"]),
                    StartDate = DateTime.MinValue,
                    EndDate   = DateTime.MaxValue,
                    IsDefault = true,
                });
            }

            if (rates != null && rates.Price != System.Convert.ToDecimal(ConfigurationManager.AppSettings["DefaultPrice"]))
            {
                var defaultRate = _rateService.GetItem(x => x.IsDefault);
                if (defaultRate != null)
                {
                    defaultRate.Price =
                        System.Convert.ToDecimal(ConfigurationManager.AppSettings["DefaultPrice"]);

                    _rateService.Update(defaultRate);
                }
            }
        }
Esempio n. 7
0
        public void AddTest()
        {
            var name    = Guid.NewGuid().ToString();
            var newRate = new Rate
            {
                RateInformation = name
            };
            var AddedRate = service.Add(newRate);

            Assert.IsNotNull(AddedRate);
            Assert.IsTrue(AddedRate.Id > 0);
            Assert.AreEqual(AddedRate.RateInformation, name);
        }