Exemplo n.º 1
0
        public async Task <ActionResult> EditListing(EditListingViewModel viewModel)
        {
            var temp = ctx.BookListings.Where(a => a.ID == viewModel.ID && a.Status == 0).FirstOrDefault();
            // Find all open transactions and remove them
            var openLogs = ctx.TransactionLogs.Where(a => a.SellerID == temp.ApplicationUserID && a.BookID == temp.BookID && a.Condition == temp.Condition && a.SoldPrice == temp.AskingPrice);

            foreach (TransactionLog log in openLogs)
            {
                ctx.TransactionLogs.Remove(log);
            }

            Course newCourse = new Course();

            temp.Condition   = viewModel.condition;
            temp.CourseID    = viewModel.courseID;
            temp.AskingPrice = viewModel.price;
            // If they created a new course, add the course to the database.
            if (viewModel.newCourse)
            {
                newCourse.Dept      = viewModel.courseDept;
                newCourse.CourseNum = viewModel.courseID;
                newCourse.Name      = viewModel.courseName;
                ApplicationUser user = await usrCtx.GetUserAsync(HttpContext.User);

                newCourse.SchoolID = user.SchoolID;
                ctx.Courses.Add(newCourse);
                temp.Course = newCourse;
            }

            ctx.Update(temp);
            ctx.SaveChanges();

            // Put it after the new course is added and saved into the database so it receives an ID.
            if (viewModel.newCourse)
            {
                // Connect the book and the new course if it doesn't already exist.
                if (!ctx.BookToCourses.Where(a => a.BookID == viewModel.book.ID && a.CourseID == newCourse.ID).Any())
                {
                    BookToCourse bookToCourse = new BookToCourse();
                    bookToCourse.BookID   = viewModel.book.ID;
                    bookToCourse.CourseID = newCourse.ID;
                    ctx.BookToCourses.Add(bookToCourse);
                    ctx.SaveChanges();
                }
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> WishList(EditListingViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user = await usrCtx.GetUserAsync(HttpContext.User);

                Course      newCourse  = new Course();
                BookListing newListing = new BookListing();
                newListing.ApplicationUserID = user.Id;
                newListing.Condition         = viewModel.condition;
                newListing.BookID            = viewModel.book.ID;
                newListing.CourseID          = viewModel.courseID;
                newListing.AskingPrice       = viewModel.price;
                newListing.Type = WISHLIST;
                // If they created a new course, add the course to the database.
                if (viewModel.newCourse)
                {
                    newCourse.Dept      = viewModel.courseDept;
                    newCourse.CourseNum = viewModel.courseID;
                    newCourse.Name      = viewModel.courseName;
                    newCourse.SchoolID  = user.SchoolID;
                    ctx.Courses.Add(newCourse);
                    newListing.Course = newCourse;
                }
                else
                {
                    // Connect the book and the  pre-existing course if it doesn't already exist.
                    if (!ctx.BookToCourses.Where(a => a.BookID == viewModel.book.ID && a.CourseID == viewModel.courseID).Any())
                    {
                        BookToCourse bookToCourse = new BookToCourse();
                        bookToCourse.BookID   = viewModel.book.ID;
                        bookToCourse.CourseID = viewModel.courseID;
                        ctx.BookToCourses.Add(bookToCourse);
                    }
                }

                ctx.BookListings.Add(newListing);
                ctx.SaveChanges();

                // Put it after the new course is added and saved into the database so it receives an ID.
                if (viewModel.newCourse)
                {
                    // Connect the book and the new course if it doesn't already exist.
                    if (!ctx.BookToCourses.Where(a => a.BookID == viewModel.book.ID && a.CourseID == newCourse.ID).Any())
                    {
                        BookToCourse bookToCourse = new BookToCourse();
                        bookToCourse.BookID   = viewModel.book.ID;
                        bookToCourse.CourseID = newCourse.ID;
                        ctx.BookToCourses.Add(bookToCourse);
                        ctx.SaveChanges();
                    }
                }

                return(RedirectToAction("Index", "Profile"));
            }
            else
            {
                viewModel.book    = ctx.Books.Where(a => a.ID == viewModel.book.ID).FirstOrDefault();
                viewModel.courses = ctx.Courses.ToList();
                return(View(viewModel));
            }
        }