Exemple #1
0
        public async Task <ManagerDTO> SaveManager(ManagerDTO postedManager)
        {
            bool isUpdate = (postedManager.ManagerId > 0); // New or Update?
            // map DTO to manager entity
            Manager mgr = ManagerHelper.GetManager(postedManager);

            mgr.CreatedDate = DateTime.Now;

            // We should always use transaction for operations involving two or more entities
            using (var transaction = await context.Database.BeginTransactionAsync())
            {
                try
                {
                    // first save manager entity
                    if (isUpdate)
                    {
                        mgr.ManagerId = postedManager.ManagerId;
                        context.Manager.Update(mgr);
                        context.SaveChanges();
                    }
                    else
                    {
                        context.Manager.Add(mgr);
                        context.SaveChanges();
                    }
                    // then save address entity
                    Address address = ManagerHelper.GetAddress(postedManager);
                    address.ParentId = mgr.ManagerId;

                    if (isUpdate)
                    {
                        address.AddressId = postedManager.AddressId;
                        context.Address.Update(address);
                        context.SaveChanges();
                    }
                    else
                    {
                        address.AddressId = 0;
                        context.Address.Add(address);
                        context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }

                transaction.Commit();
                postedManager.ManagerId = mgr.ManagerId; //used for image filename
                return(postedManager);
            }
        }
Exemple #2
0
        /// <summary>
        /// Saves the user profile with the given {id}
        /// </summary>
        /// <returns>the user profile with the given id</returns>
        public async Task <CustomerProfileDTO> UpdateProfile(CustomerProfileDTO model)
        {
            // retrieve the profile to edit
            try
            {
                var customerProfile = await dbContext.CustomerProfile.FirstOrDefaultAsync(p => p.Id == model.Id);

                if (customerProfile == null)
                {
                    //TODO: return not found code
                }


                //handle the update with auto-mapper
                customerProfile =
                    mapper.Map(model, customerProfile);

                //properties set from server

                customerProfile.UpdatedDate = DateTime.Now;

                dbContext.SaveChanges();

                return(mapper.Map <CustomerProfileDTO>(customerProfile));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }