Exemple #1
0
        public ActionResult Edit(Book book)
        {
            try
            {
                if (book.Type == null)
                {
                    book.Type = book.TypeHidden;
                }



                if (book.isPay > 0)
                {
                    book.IdAccount    = book.IdAccountHidden;
                    book.RepeatPeriod = book.RepeatPeriodHidden;
                }
                else
                {
                    if ((book.Repeat == 1) && (book.RepeatPeriod == Book.BookRepeatPeriod.NoRepeat))
                    {
                        ModelState.AddModelError("", "Repeat Period is necessary");
                    }
                }

                if (ModelState.IsValid == false)
                {
                    User user = (User)Session["User"];

                    var listCategories = _categoryRepository.GetByUser(user.IdUser);
                    var listAccounts   = _accountRepository.GetByUser(user.IdUser);

                    ViewBag.listCategories = new SelectList(listCategories, "IdCategory", "Description", "Selecione uma categoria");
                    ViewBag.listAccounts   = new SelectList(listAccounts, "IdAccount", "Description", "Selecione uma conta");


                    return(View(book));
                }


                _bookRepository.Update(book);
                _bookRepository.SaveChanges();

                if (book.isPay == 0)
                {
                    if (book.NParcels > 0)
                    {
                        var parcels = _parcelRepository.GetByBook(book.IdBook).ToList();
                        foreach (Parcel pc in parcels)
                        {
                            _parcelRepository.Remove(pc.IdParcel);
                        }
                        _parcelRepository.SaveChanges();

                        Parcel parcel = new Parcel();
                        parcel.IdBook = book.IdBook;
                        for (int i = 1; i <= book.NParcels; i++)
                        {
                            parcel.ParcelNumber = i;
                            _parcelRepository.Add(parcel);
                            _parcelRepository.SaveChanges();
                        }
                    }
                }



                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Não foi possível salvar as mudanças. Tente Novamente.");
                return(View());
            }
        }