Exemplo n.º 1
0
        public static BookStoreAdmin.ViewModels.Book AddBook(BookStoreAdmin.ViewModels.BookRequest objBookRequest)
        {
            BookStoreAdmin.Models.book book = new book();

            BookStoreAdmin.ViewModels.Book objBook = new ViewModels.Book();

            using (BookStoreAdmin.Models.BookStoreAdminEntities1 context = new BookStoreAdminEntities1())
            {
                book.title          = objBookRequest.title;
                book.description    = objBookRequest.description;
                book.fk_category_id = objBookRequest.category;
                book.price          = objBookRequest.price;

                foreach (int authorId in objBookRequest.authors)
                {
                    book.authors.Add(new Models.author()
                    {
                        id = authorId
                    });
                }
                book.created_on = new DateTimeOffset();

                context.books.Add(book);

                foreach (BookStoreAdmin.Models.author author in book.authors)
                {
                    context.Entry(author).State = System.Data.Entity.EntityState.Unchanged;
                }

                context.SaveChanges();
            }

            using (BookStoreAdmin.Models.BookStoreAdminEntities1 context = new BookStoreAdminEntities1())
            {
                objBook = context.books
                          .Where(b => b.id == book.id)
                          .Select(book1 => new BookStoreAdmin.ViewModels.Book()
                {
                    id          = book1.id,
                    title       = book1.title,
                    description = book1.description,
                    category    = new ViewModels.Category()
                    {
                        id = book1.category.id, description = book1.category.description, name = book1.category.name
                    },
                    authors = book1.authors.Select((author) => new BookStoreAdmin.ViewModels.Author()
                    {
                        id          = author.id,
                        description = author.description,
                        first_name  = author.first_name,
                        last_name   = author.last_name
                    }).ToList(),
                    price = book1.price
                }).Single();
            }

            return(objBook);
        }
Exemplo n.º 2
0
        public static BookStoreAdmin.ViewModels.Category EditCategory(BookStoreAdmin.ViewModels.Category category)
        {
            BookStoreAdmin.Models.category objCategory = new Models.category();

            using (BookStoreAdmin.Models.BookStoreAdminEntities1 context = new BookStoreAdminEntities1())
            {
                objCategory             = context.categories.Find(category.id);
                objCategory.name        = category.name;
                objCategory.description = category.description;
                context.SaveChanges();
            }
            return(category);
        }
Exemplo n.º 3
0
        public static void AddUser(BookStoreAdmin.ViewModels.UserRequest user)
        {
            BookStoreAdmin.Models.user objUser = new Models.user();

            using (BookStoreAdmin.Models.BookStoreAdminEntities1 context = new BookStoreAdminEntities1())
            {
                objUser.first_name = user.first_name;
                objUser.last_name  = user.last_name;
                objUser.user_name  = user.user_name;
                objUser.password   = user.password;
                context.users.Add(objUser);
                context.SaveChanges();
            }
        }
Exemplo n.º 4
0
        public static BookStoreAdmin.ViewModels.Author EditAuthor(BookStoreAdmin.ViewModels.Author author)
        {
            BookStoreAdmin.Models.author objAuthor = new Models.author();

            using (BookStoreAdmin.Models.BookStoreAdminEntities1 context = new BookStoreAdminEntities1())
            {
                objAuthor             = context.authors.Find(author.id);
                objAuthor.first_name  = author.first_name;
                objAuthor.last_name   = author.last_name;
                objAuthor.description = author.description;
                context.SaveChanges();
            }
            return(author);
        }
Exemplo n.º 5
0
        public static BookStoreAdmin.ViewModels.Book EditBook(BookStoreAdmin.ViewModels.BookRequest objBookRequest)
        {
            BookStoreAdmin.Models.book book = new Models.book();
            List <int> intAuthorids         = new List <int>();

            BookStoreAdmin.ViewModels.Book objBook = new ViewModels.Book();

            using (BookStoreAdmin.Models.BookStoreAdminEntities1 context = new BookStoreAdminEntities1())
            {
                book = context.books.Include("authors").Single(b => b.id == objBookRequest.id);

                foreach (var author in book.authors.ToList())
                {
                    if (!objBookRequest.authors.Contains(author.id))
                    {
                        book.authors.Remove(author);
                    }
                }

                foreach (var newAuthorId in objBookRequest.authors)
                {
                    // Add the roles which are not in the list of user's roles
                    if (!book.authors.Any(r => r.id == newAuthorId))
                    {
                        var newAuthor = new author {
                            id = newAuthorId
                        };
                        context.authors.Attach(newAuthor);
                        book.authors.Add(newAuthor);
                    }
                }

                book.title          = objBookRequest.title;
                book.description    = objBookRequest.description;
                book.price          = objBookRequest.price;
                book.fk_category_id = objBookRequest.category;
                context.SaveChanges();
            }


            using (BookStoreAdmin.Models.BookStoreAdminEntities1 context = new BookStoreAdminEntities1())
            {
                objBook = context.books
                          .Where(b => b.id == book.id)
                          .Select(book1 => new BookStoreAdmin.ViewModels.Book()
                {
                    id          = book1.id,
                    title       = book1.title,
                    description = book1.description,
                    category    = new ViewModels.Category()
                    {
                        id = book1.category.id, description = book1.category.description, name = book1.category.name
                    },
                    authors = book1.authors.Select((author) => new BookStoreAdmin.ViewModels.Author()
                    {
                        id          = author.id,
                        description = author.description,
                        first_name  = author.first_name,
                        last_name   = author.last_name
                    }).ToList(),
                    price = book1.price
                }).Single();
            }

            return(objBook);
        }