Ejemplo n.º 1
0
        public async Task <long?> EnterpriseUpdate(long id, EnterpriseUpdateModel model)
        {
            try
            {
                var enterprise = new Enterprise();

                using (var ctx = new dbPumox())
                {
                    using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                    {
                        enterprise = await ctx.Enterprises.Where(x => x.Id == id).FirstOrDefaultAsync();

                        if (enterprise == null)
                        {
                            return(null);
                        }

                        enterprise.Name = model.Name;
                        enterprise.EstablishmentYear = model.EstablishmentYear;
                        CreateOrUpdateEmployee(model, enterprise);
                        await ctx.SaveChangesAsync();

                        scope.Complete();
                    }
                }

                return(enterprise.Id);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
Ejemplo n.º 2
0
        public async Task <long?> CompanyCreate(EnterpriseCreateModel model)
        {
            long id = 0;

            try
            {
                using (var ctx = new dbPumox())
                {
                    using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                    {
                        Enterprise enterprise = new Enterprise()
                        {
                            Name = model.Name,
                            EstablishmentYear = model.EstablishmentYear,
                            Employees         = new List <Employee>()
                        };

                        if (model.Employees != null)
                        {
                            foreach (var employee in model.Employees)
                            {
                                enterprise.Employees.Add(new Employee
                                {
                                    FirstName   = employee.Firstname,
                                    LastName    = employee.Lastname,
                                    DateOfBirth = employee.DateOfBirth,
                                    JobTitle    = employee.JobTitle,
                                });
                            }
                        }

                        ctx.Enterprises.Add(enterprise);
                        await ctx.SaveChangesAsync();

                        scope.Complete();
                        id = enterprise.Id;
                    }
                }
                return(id);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }