Esempio n. 1
0
        public ResponseVM Create(BookVM bookVM)
        {
            using (context)
            {
                using (var dbTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        bookVM.MyGuid = Guid.NewGuid();
                        var bookSaved = context.Books.Add(toModel.Book(bookVM)).Entity;
                        context.SaveChanges();

                        foreach (var authID in bookVM.AuthorIdList)
                        {
                            //validate existence of author
                            var author = context.Authors.Find(authID);
                            if (author == null)
                            {
                                return(new ResponseVM("create", false, "Book", "Author does not exists"));
                            }
                            var bookAuthor = new BookAuthor
                            {
                                AuthorID       = authID,
                                BookID         = bookSaved.ID,
                                AuthorFullName = toViewModel.ToFullName(author.FirstName, author.MiddleName, author.LastName)
                            };

                            context.BookAuthors.Add(bookAuthor);
                            context.SaveChanges();
                        }

                        //commit changes to db
                        dbTransaction.Commit();
                        return(new ResponseVM
                                   ("create", true, "Book"));
                    }
                    catch (Exception ex)
                    {
                        //rollback
                        dbTransaction.Rollback();
                        return(new ResponseVM("create", false, "Book", ResponseVM.SOMTHING_WENT_WRONG, "", ex));
                    }
                }
            }
        }
Esempio n. 2
0
        public ResponseVM Create(BookVM bookVM)
        {
            using (context)
            {
                using (var dbTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        bookVM.MyGuid = Guid.NewGuid();
                        // saves the book first then assign the entity to bookSaved where we will get the generated id
                        var bookSaved = context.Books.Add(toModel.Book(bookVM)).Entity;
                        context.SaveChanges();

                        foreach (var authID in bookVM.AuthorIdList)
                        {
                            // validates existence of author
                            var author = context.Authors.Find(authID);
                            if (author == null)
                            {
                                return(new ResponseVM("created", false, "Book", "Author does not exists"));
                            }
                            // saves to bookauthor
                            var bookAuthor = new BookAuthor
                            {
                                AuthorID       = authID,
                                BookID         = bookSaved.ID,
                                AuthorFullName = $"{author.FirstName}{ (string.IsNullOrEmpty(author.MiddleName) ? "" : " " + author.MiddleName) }{(string.IsNullOrEmpty(author.LastName) ? "" : " " + author.LastName)}"
                            };
                            context.BookAuthors.Add(bookAuthor);
                            context.SaveChanges();
                        }

                        // commits changes to db
                        dbTransaction.Commit();
                        return(new ResponseVM("created", true, "Book"));
                    }
                    catch (Exception ex)
                    {
                        // rollback any changes
                        dbTransaction.Rollback();
                        return(new ResponseVM("created", false, "Book", ResponseVM.SOMETHING_WENT_WRONG, "", ex));
                    }
                }
            }
        }
Esempio n. 3
0
        public ResponseVM Create(BookVM bookVM)
        {
            using (context)
            {
                using (var dbTransaction = context.Database.BeginTransaction())
                    try
                    {
                        bookVM.MyGuid = Guid.NewGuid();
                        context.Books.Add(toModel.Book(bookVM));
                        context.SaveChanges();

                        //commit change to db
                        dbTransaction.Commit();
                        return(new ResponseVM("create", true, "Book"));
                    }
                    catch (Exception ex)
                    {
                        //rollback changes
                        dbTransaction.Rollback();
                        return(new ResponseVM("create", false, "Book", ResponseVM.SOMETHING_WENT_WRONG, "", ex));
                    }
            }
        }