Ejemplo n.º 1
0
        public IHttpActionResult PostBookshelf(BookshelfCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            BookshelfService service = CreateBookshelfService();

            if (!service.CreateBookshelf(model))
            {
                return(InternalServerError());
            }
            return(Ok());
        }
        public bool CreateBookshelf(BookshelfCreate model)
        {
            var bookshelfToCreate =
                new Bookshelf()
            {
                UserId     = _userId,
                ShelfName  = model.ShelfName,
                CreatedUtc = DateTimeOffset.Now
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Bookshelves.Add(bookshelfToCreate);
                return(ctx.SaveChanges() == 1);
            }
        }
Ejemplo n.º 3
0
        public bool CreateBookshelf(BookshelfCreate model)
        {
            UserProfile profile  = _context.UserProfiles.Single(p => p.OwnerId == _userId);
            List <Book> allBooks = _context.Books.ToList();

            Bookshelf shelf = new Bookshelf()
            {
                Title         = model.Title,
                OwnerId       = _userId,
                BookIds       = model.BookIds,
                Books         = allBooks.Where(b => model.BookIds.Contains(b.BookId)).ToList(),
                UserProfileId = profile.UserProfileId
            };

            _context.Bookshelves.Add(shelf);
            return(_context.SaveChanges() > 0);
        }
        public ActionResult Create(BookshelfCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateBookshelfService();

            if (service.CreateBookshelf(model))
            {
                TempData["SaveResult"] = "Your bookshelf was created.";
                return(RedirectToAction("Index"));
            }
            ;

            ModelState.AddModelError("", "Bookshelf could not be created.");

            return(View(model));
        }