public void AddAccountPeriod(AccountPeriod accountPeriod) { if (accountPeriod == null) { throw new ArgumentNullException("accountPeriod"); } using (var dbContext = this.CreateDbContext()) { var entityAccountPeriod = this.ToEntity(accountPeriod); dbContext.AccountPeriods.InsertOnSubmit(entityAccountPeriod); foreach (var shift in accountPeriod.Shifts) { var entityShift = dbContext.Shifts.FirstOrDefault(x => x.Id == shift.Id); if (entityShift == null) { throw new InvalidOperationException("Shift does not exist"); } dbContext.AccountShifts .InsertOnSubmit(new AccountShift { AccountPeriod = entityAccountPeriod, AccountPeriodId = accountPeriod.Id, Shift = entityShift, ShiftId = shift.Id }); } dbContext.SubmitChanges(); } }
private EntityAccountPeriod ToEntity(AccountPeriod accountPeriod) { return new EntityAccountPeriod { Id = accountPeriod.Id, Title = accountPeriod.Title, AvailableSlot = accountPeriod.AvailableSlot, BeginPeriod = accountPeriod.BeginPeriod, EndPeriod = accountPeriod.EndPeriod, UserId = accountPeriod.UserId, IsEnabled = accountPeriod.IsEnabled, IsBlocked = accountPeriod.IsBlocked, WorkingDay = (int)accountPeriod.WorkingDay }; }
private void MapChange(BankProjectModelsDataContext dbContext, AccountPeriod accountPeriod, EntityAccountPeriod existEntity) { existEntity.Title = accountPeriod.Title; existEntity.AvailableSlot = accountPeriod.AvailableSlot; existEntity.BeginPeriod = accountPeriod.BeginPeriod; existEntity.EndPeriod = accountPeriod.EndPeriod; existEntity.UserId = accountPeriod.UserId; existEntity.IsEnabled = accountPeriod.IsEnabled; existEntity.IsBlocked = accountPeriod.IsBlocked; existEntity.WorkingDay = (int)accountPeriod.WorkingDay; var currentShiftIds = accountPeriod.Shifts.Select(x => x.Id).ToArray(); var originalShifts = existEntity.AccountShifts.ToList(); foreach (var accountShift in originalShifts) { if (!currentShiftIds.Contains(accountShift.ShiftId)) { existEntity.AccountShifts.Remove(accountShift); dbContext.AccountShifts.DeleteOnSubmit(accountShift); } } foreach (var shiftId in currentShiftIds) { if (existEntity.AccountShifts.All(x => x.ShiftId != shiftId)) { var newAccountShift = new AccountShift { AccountPeriodId = accountPeriod.Id, ShiftId = shiftId }; existEntity.AccountShifts.Add(newAccountShift); dbContext.AccountShifts.InsertOnSubmit(newAccountShift); } } }
public void UpdateAccountPeriod(AccountPeriod accountPeriod) { if (accountPeriod == null) { throw new ArgumentNullException("accountPeriod"); } using (var dbContext = this.CreateDbContext()) { var existEntity = dbContext.AccountPeriods.FirstOrDefault(x => x.Id == accountPeriod.Id); if (existEntity == null) { throw new InvalidOperationException(); } this.MapChange(dbContext, accountPeriod, existEntity); // Remove account shift foreach (var accountShift in existEntity.AccountShifts) { dbContext.AccountShifts.DeleteOnSubmit(accountShift); } existEntity.AccountShifts.Clear(); // Add new account shift foreach (var shift in accountPeriod.Shifts) { var entityShift = dbContext.Shifts.FirstOrDefault(x => x.Id == shift.Id); if (entityShift == null) { throw new InvalidOperationException("Shift does not exist"); } dbContext.AccountShifts .InsertOnSubmit(new AccountShift { AccountPeriod = existEntity, AccountPeriodId = accountPeriod.Id, ShiftId = shift.Id, Shift = entityShift }); } dbContext.SubmitChanges(); } }