예제 #1
0
파일: Book_Tests.cs 프로젝트: teo-mateo/sdc
        public void AddBook_Test()
        {
            BookController c = CreateController<BookController>();
            BookViewModel vm; int bookCount;

            using (var db = new SDCContext())
            {
                bookCount = db.Books.Count();

                var profile = db.UserProfiles.Find(1);
                var shelf = db.Shelves.FirstOrDefault(p => p.Owner.UserId == profile.UserId);
                var twoGenres = db.Genres.Take(2)
                    .ToList();

                var twoAuthors = db.Authors.OrderBy(a => Guid.NewGuid().ToString())
                    .Take(2)
                    .ToList();

                var publisher = db.Publishers.Take(1)
                    .First();

                var language = db.Languages
                    .Where(l=>l.IsVisible)
                    .OrderBy(l => Guid.NewGuid().ToString())
                    .First();

                vm = new BookViewModel()
                {
                    Title = Guid.NewGuid().ToString(),
                    Year = 2015,
                    Genres = twoGenres,
                    Authors = twoAuthors,
                    Description = "Lorem ipsum",
                    Publisher = publisher,
                    Language = language,
                    ShelfId = shelf.Id,
                    ShelfName = shelf.Name,
                    ISBN = Guid.NewGuid().ToString(),
                    AddedDate = DateTime.Now
                };
            }

            c.AddBook(vm);
            Assert.AreEqual(bookCount + 1, new SDCContext().Books.Count());
        }
예제 #2
0
        public JsonResult AddBook(BookViewModel bookViewModel)
        {
            var profile = (UserProfile)Session["UserInfo"];
            if (!User.Identity.IsAuthenticated || profile == null)
            {
                //STUPID
                return Json(new { id = -1 });
            }

            int id = 0;

            using (var db = new SDCContext())
            {
                db.AttachProfile(profile);

                //verify that the shelf exists and it belongs to the logged in user
                var shelf = db.Shelves.Include(o => o.Owner).FirstOrDefault(s => s.Id == bookViewModel.ShelfId);
                if (shelf == null || shelf.Owner.UserId != profile.UserId)
                {
                    //STUPID
                    return Json(new { id = -1 });
                }

                Book book = AutoMapper.Mapper.Map<Book>(bookViewModel);
                book.Shelf = shelf;
                book.AddedDate = DateTime.Now;
                Book.MapComplexProperties(db, book, bookViewModel, profile);

                db.Books.Add(book);
                db.SaveChanges();
                id = book.Id;

                //activity
                SDC.Library.Helpers.ActivityHelper.Activity_BookAdded(
                    db, profile, book,
                    Url.Action("ViewBook", "Book", new { id = book.Id }),
                    Url.Action("Details", "Shelves", new { id = book.Shelf.Id }));
            }

            return Json(new { id = id });
        }
예제 #3
0
        public ActionResult UpdateBook(BookViewModel bookViewModel)
        {
            var profile = ((UserProfile)Session["UserInfo"]);
            if (!User.Identity.IsAuthenticated || profile == null)
                return RedirectToAction("Index", "Home");

            try
            {
                using (var db = new SDCContext())
                {
                    db.AttachProfile(profile);

                    var book = db.Books
                        .Include(b=>b.Authors)
                        .Include(b=>b.Genres)
                        .Include(b=>b.Publisher)
                        .Include(b=>b.Language)
                        .Include(b=>b.Shelf)
                        .First(b => b.Id == bookViewModel.Id);

                    AutoMapper.Mapper.Map<BookViewModel, Book>(bookViewModel, book);

                    Book.MapComplexProperties(db, book, bookViewModel, profile);

                    db.SaveChanges();

                    //activity
                    SDC.Library.Helpers.ActivityHelper.Activity_BookUpdated(
                        db, profile, book,
                        Url.Action("ViewBook", "Book", new { id = book.Id }),
                        Url.Action("Details", "Shelves", new { id = book.Shelf.Id }));

                    return new HttpStatusCodeResult(HttpStatusCode.OK);
                }
            }
            catch(Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
        }
예제 #4
0
        public JsonResult GetNewBook()
        {
            BookViewModel b = new BookViewModel()
            {
                Title = "new book",
                Authors = new List<Author>()
                {
                    new Author() { Id = 0, Name = "Mihail Sadoveanu" }
                },
                Publisher = new Publisher() { Id = 0, Name = "Pearson, Specter, Litt"},
                Genres = new List<Genre>()
                {
                    new Genre() {Id = 1, Name = "Science fiction" }
                },
                ISBN = "ISBN UNKNOWN",
                Year = 1984
            };

            return Json(b, JsonRequestBehavior.AllowGet);
        }
예제 #5
0
파일: Book.cs 프로젝트: teo-mateo/sdc
        public static void MapComplexProperties(SDCContext db, Book book, BookViewModel bookViewModel, UserProfile profile)
        {
            #region Authors entities
            var auth_to_remove = book.Authors.Where(a => !bookViewModel.Authors.Any(a2 => a2.Id == a.Id)).ToList();
            var auth_to_add = bookViewModel.Authors.Where(a => !book.Authors.Any(a2 => a2.Id == a.Id)).ToList();
            auth_to_remove.ForEach(a => book.Authors.Remove(a));
            auth_to_add.ForEach(a => book.Authors.Add(a));

            foreach (Author a in book.Authors)
            {
                if (a.Id == 0)
                {
                    a.AddedDate = DateTime.Now;
                    a.AddedBy = profile;
                    db.Entry<Author>(a).State = EntityState.Added;
                }
                else
                {
                    db.Attach(a);
                }
            }
            #endregion

            #region Genres entities
            var genres_to_remove = book.Genres.Where(g => !bookViewModel.Genres.Any(g2 => g2.Id == g.Id)).ToList();
            var genres_to_add = bookViewModel.Genres.Where(g => !book.Genres.Any(g2 => g2.Id == g.Id)).ToList();
            genres_to_remove.ForEach(g => book.Genres.Remove(g));
            genres_to_add.ForEach(g => book.Genres.Add(g));

            foreach (var g in book.Genres)
            {
                db.Attach(g);
            }
            #endregion

            #region Publisher entity

            if (bookViewModel.Publisher != null)
            {
                db.Attach(bookViewModel.Publisher);
                book.Publisher = bookViewModel.Publisher;
            }
            else
                book.Publisher = null;

            #endregion

            #region Language
            var lang = bookViewModel.Language;
            db.AttachCodeEntity(ref lang);
            book.Language = lang;

            #endregion
        }