public async Task <double> UpdateProfileAmountAsync(int employeeId, int profileId, double amount) { try { var context = new ElementContext(); var profile = context.Profiles.Find(profileId); if (profile != null) { if (profile.Count > amount && amount > 0) { profile.Amount = amount; profile.ElementInfos.Add(new ElementInfo() { Amount = amount, EmployeeId = employeeId, Time = DateTime.Now }); context.Entry(profile).State = EntityState.Modified; var result = await context.SaveChangesAsync(); if (result > 0) { return(await Task.FromResult(profile.Amount)); } } } return(await Task.FromResult <double>(-1)); } catch (Exception ex) { PrintException(ex); throw new FaultException(ex.Message, new FaultCode("UpdateProfileAmountAsync"), "UpdateProfileAmountAsync"); } }
public async Task <bool> DeleteProfileAsync(int profileId) { try { using (var context = new ElementContext()) { var profile = context.Profiles.SingleOrDefault(x => x.ProfileId == profileId); Console.WriteLine($"Name: {profile.ProfileNumber}"); context.Entry(profile).State = EntityState.Deleted; return(await context.SaveChangesAsync() > 0); } } catch (Exception ex) { PrintException(ex); throw new FaultException(ex.Message, new FaultCode("DeleteProfileAsync"), "DeleteProfileAsync"); } }
/* * var context = new EmployeeContext(); * var entity = AutoMapperConfiguration.Mapper.Map<Division>(division); * var result = context.Divisions.Find(division.DivisionId); * if (result != null) * { * result.Description = entity.Description; * result.DivisionInfoId = entity.DivisionInfoId; * result.DivisionType = entity.DivisionType; * result.Name = entity.Name; * context.Entry(result).State = EntityState.Modified; * context.Entry(result.DivisionType).State = EntityState.Modified; * return await context.SaveChangesAsync(); * } * else * { * context.Divisions.Attach(entity); * context.Divisions.Add(entity); * * var divisionId = await context.SaveChangesAsync(); * if (divisionId > 0) * { * return await Task.FromResult(entity.DivisionId); * } * else * { * return await Task.FromResult(0); * } * } */ public async Task <bool> UpdateProfileAsync(int employeeId, ProfileDTO profile) { try { var context = new ElementContext(); var entity = AutoMapperConfiguration.Mapper.Map <Profile>(profile); var result = context.Profiles.Find(profile.ProfileId); if (result != null) { if (result.Amount != entity.Amount) { result.ElementInfos.Add(new ElementInfo { Amount = entity.Amount, Time = DateTime.Now, EmployeeId = employeeId }); } result.Amount = entity.Amount; result.Contraction = entity.Contraction; result.Count = entity.Count; result.Description = entity.Description; result.Length = entity.Length; result.ProfileNumber = entity.ProfileNumber; result.Surface = entity.Surface; context.Entry(result).State = EntityState.Modified; if (await context.SaveChangesAsync() > 0) { return(await Task.FromResult(true)); } } return(await Task.FromResult(false)); } catch (Exception ex) { PrintException(ex); throw new FaultException(ex.Message, new FaultCode("UpdateProfileAsync"), "UpdateProfileAsync"); } }
public async Task <int> CreateProfileAsync(ProfileDTO profile, int employeeId) { try { var context = new ElementContext(); var entity = AutoMapperConfiguration.Mapper.Map <Profile>(profile); if (entity != null) { if (entity.Amount > 0) { //var ei = new ElementInfo() { Amount = entity.Amount, EmployeeId = employeeId, Time = DateTime.Now }; //var elementInfo = context.ElementInfos.Add(ei); entity.ElementInfos?.Add(new ElementInfo() { Amount = entity.Amount, EmployeeId = employeeId, Time = DateTime.Now }); } context.Profiles.Add(entity); var result = await context.SaveChangesAsync(); if (result > 0) { Console.WriteLine($"ProfileID: {entity.ProfileId}"); return(await Task.FromResult(entity.ProfileId)); } } return(await Task.FromResult(0)); } catch (Exception ex) { PrintException(ex); throw new FaultException(ex.Message, new FaultCode("CreateProfile"), "CreateProfile"); } }