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));
                    }
                }
            }
        }
Exemple #2
0
        public ResponseVM Create(EmployeeEmployeeTypeVM employeeEmployeeTypeVM)
        {
            using (context)
            {
                using (var dbTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        employeeEmployeeTypeVM.EmployeeEmployeeTypeid = Guid.NewGuid().ToString();
                        var employeeEmployeeTypeSaved = context.EmployeeEmployeeTypes.Add(toModel.EmployeeEmployeeType(employeeEmployeeTypeVM)).Entity;
                        context.SaveChanges();

                        foreach (var empID in employeeEmployeeTypeVM.EmployeeIdList)
                        {
                            var employee = context.Employees.Find(empID);
                            if (employee == null)
                            {
                                return(new ResponseVM("create", false, "EmployeeEmployeeType", "Employee does not exists"));
                            }
                            var typeEmployee = new TypeEmployee
                            {
                                EmployeeID             = empID,
                                EmployeeEmployeeTypeid = employeeEmployeeTypeSaved.EmployeeEmployeeTypeid,
                                EmployeeFullName       = toViewModel.ToFullName(employee.FirstName, employee.LastName)
                            };

                            context.TypeEmployees.Add(typeEmployee);
                            context.SaveChanges();
                        }


                        dbTransaction.Commit();
                        return(new ResponseVM
                                   ("create", true, "EmployeeEmployeeType"));
                    }
                    catch (Exception ex)
                    {
                        dbTransaction.Rollback();
                        return(new ResponseVM("create", false, "EmployeeEmployeeType", ResponseVM.SOMETHING_WENT_WRONG, "", ex));
                    }
                }
            }
        }