예제 #1
0
        public ActionResult Sell(ArticleViewModel model)
        {
            var course = Repository.GetCourseByAcronym(model.Acronym);

            // Validating the model
            if (model.Type != Article.CALCULATOR_CODE && string.IsNullOrEmpty(model.ISBN))
            {
                ModelState.AddModelError(nameof(ArticleViewModel.ISBN), "Le type choisi requiert un ISBN ou un code.");
            }

            if (course == null && model.Type != Article.CALCULATOR_CODE)
            {
                ModelState.AddModelError(nameof(ArticleViewModel.Acronym), "Le type choisi requiert un cours de la liste.");
            }

            // Proceeding to add the new offer.
            if (ModelState.IsValid)
            {
                Article newArticle  = null;
                var     uploadsPath = Server.MapPath(UPLOADS_PATH);

                switch (model.Type)
                {
                case Article.BOOK_CODE:
                    newArticle = new Book()
                    {
                        Course = course,
                        Title  = model.Title,
                        ISBN   = model.ISBN
                    };
                    break;

                case Article.COURSE_NOTES_CODE:
                    newArticle = new CourseNotes()
                    {
                        Course   = course,
                        Title    = model.Title,
                        SubTitle = "Sample Subtitle",      // FIXME: Inconsistent with Title in Article and there's no Title for Offer.
                        BarCode  = model.ISBN
                    };
                    break;

                case Article.CALCULATOR_CODE:
                    newArticle = new Calculator()
                    {
                        Title = model.Title,
                        Model = model.CalculatorModel
                    };
                    break;
                }

                var   now   = DateTime.Now;
                Offer offer = new Offer()
                {
                    StartDate     = now,
                    MarkedSoldOn  = now,
                    Price         = model.Price, // FIXME: No elements for this in the view. Weird.
                    Condition     = model.Condition,
                    Article       = newArticle,
                    ManagedByFair = false,
                    Title         = model.Title // FIXME: No elements for this in the view. Weird.
                };

                if (model.ForNextFair)
                {
                    var nextFair = Repository.GetNextFair();

                    offer.ManagedByFair = true;
                    nextFair?.Offers.Add(offer);
                }

                Repository.AddOffer(offer, toUser: User.Identity.GetUserId());

                if (Session["images"] != null)
                {
                    List <OfferImage> images = Session["images"] as List <OfferImage>;
                    foreach (var image in images)
                    {
                        image.MovePermanently(uploadsPath);
                        offer.AddImage(image);
                    }
                }

                Repository.Update();
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                model.Courses = Repository.GetAllCourses().ToList();
                return(View(model));
            }
        }