/// <summary> /// This can be used to add / update / remove slots. /// </summary> public async Task <List <Duty> > UpdateDuties(List <Duty> duties, bool overrideValidation) { if (!duties.Any()) { throw new BusinessLayerException("Didn't provide any duties."); } await CheckForDutySlotOverlap(duties, overrideValidation); var dutyIds = duties.SelectToList(duty => duty.Id); var savedDuties = Db.Duty.AsSingleQuery() .Include(d => d.DutySlots) .In(dutyIds, s => s.Id) .Where(s => s.ExpiryDate == null); foreach (var duty in duties) { var savedDuty = await savedDuties.FirstOrDefaultAsync(s => s.Id == duty.Id); savedDuty.ThrowBusinessExceptionIfNull($"{nameof(Duty)} with the id: {duty.Id} could not be found. "); duty.Timezone.GetTimezone().ThrowBusinessExceptionIfNull($"A valid {nameof(duty.Timezone)} must be provided."); Db.Entry(savedDuty !).CurrentValues.SetValues(duty); Db.Entry(savedDuty).Property(x => x.LocationId).IsModified = false; Db.Entry(savedDuty).Property(x => x.ExpiryDate).IsModified = false; foreach (var dutySlot in duty.DutySlots) { var savedDutySlot = await Db.DutySlot .FirstOrDefaultAsync(ds => ds.Id == dutySlot.Id && ds.ExpiryDate == null); dutySlot.LocationId = duty.LocationId; dutySlot.DutyId = duty.Id; dutySlot.Timezone = duty.Timezone; dutySlot.Duty = null; dutySlot.Sheriff = null; dutySlot.Location = null; if (savedDutySlot == null) { await Db.DutySlot.AddAsync(dutySlot); } else { Db.Entry(savedDutySlot).CurrentValues.SetValues(dutySlot); } } Db.RemoveRange(savedDuty.DutySlots.Where(ds => duty.DutySlots.All(d => d.Id != ds.Id))); } await Db.SaveChangesAsync(); var sheriffsForDuties = duties.SelectMany(d => d.DutySlots).SelectDistinctToList(ds => ds.SheriffId); var firstDuty = duties.First(); await HandleShiftAdjustmentsAndOvertime(firstDuty.LocationId, firstDuty.StartDate, firstDuty.Timezone, sheriffsForDuties); await Db.SaveChangesAsync(); return(await savedDuties.ToListAsync()); }
public ProductDTO Edit(int id, ProductDTO entity) { _context.Entry(_context.Products.FirstOrDefault(x => x.Id == id) !).CurrentValues .SetValues(entity); var product = _context.Products.Find(id); var productDto = _mapper.Map <ProductDTO>(product); return(productDto); }
public CategoryDTO Edit(int id, CategoryDTO entity) { _context.Entry(_context.Categories.FirstOrDefault(x => x.Id == id) !).CurrentValues .SetValues(entity); var category = _context.Categories.Find(id); var categoryDto = _mapper.Map <CategoryDTO>(category); return(categoryDto); }
public PhotoDTO Edit(int id, PhotoDTO entity) { _context.Entry(_context.Photos.FirstOrDefault(x => x.Id == id) !).CurrentValues .SetValues(entity); var photo = _context.Photos.Find(id); var photoDto = _mapper.Map <PhotoDTO>(photo); return(photoDto); }
public OrderDTO Edit(int id, OrderDTO entity) { _context.Entry(_context.Orders.FirstOrDefault(x => x.Id == id) !).CurrentValues .SetValues(entity); var order = _context.Orders.Find(id); var orderDto = _mapper.Map <OrderDTO>(order); return(orderDto); }
public async Task <List <Shift> > UpdateShifts(DutyRosterService dutyRosterService, List <Shift> shifts) { var overlaps = await GetShiftConflicts(shifts); if (overlaps.Any()) { throw new BusinessLayerException(overlaps.SelectMany(ol => ol.ConflictMessages).ToStringWithPipes()); } var shiftIds = shifts.SelectToList(s => s.Id); var savedShifts = Db.Shift.In(shiftIds, s => s.Id); if (shifts.Any(s => s.StartDate >= s.EndDate)) { throw new BusinessLayerException($"{nameof(Shift)} Start date cannot come after end date."); } if (shifts.Any(s => s.Timezone.GetTimezone() == null)) { throw new BusinessLayerException($"A valid {nameof(Shift.Timezone)} needs to be included in the {nameof(Shift)}."); } shifts = SplitLongShifts(shifts); foreach (var shift in shifts) { //Need to add shifts, because some of the shifts were split. if (shift.Id == 0) { await AddShift(shift); continue; } var savedShift = savedShifts.FirstOrDefault(s => s.Id == shift.Id); savedShift.ThrowBusinessExceptionIfNull($"{nameof(Shift)} with the id: {shift.Id} could not be found."); Db.Entry(savedShift !).CurrentValues.SetValues(shift); Db.Entry(savedShift).Property(x => x.LocationId).IsModified = false; Db.Entry(savedShift).Property(x => x.ExpiryDate).IsModified = false; savedShift.Sheriff = await Db.Sheriff.FindAsync(shift.SheriffId); savedShift.AnticipatedAssignment = await Db.Assignment.FindAsync(shift.AnticipatedAssignmentId); } await Db.SaveChangesAsync(); await CalculateOvertimeHoursForShifts(shifts); await dutyRosterService.AdjustDutySlots(shifts); return(await savedShifts.ToListAsync()); }
/// <summary> /// Adds the or update. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dbSet">The database set.</param> /// <param name="key">The key.</param> /// <param name="data">The data.</param> /// <exception cref="Exception"></exception> public static void AddOrUpdate <T>(this DbSet <T> dbSet, Expression <Func <T, object> > key, T data) where T : class { if (data == null) { return; } var context = dbSet.GetContext(); var ids = context.Model.FindEntityType(typeof(T)).FindPrimaryKey().Properties.Select(x => x.Name); var t = typeof(T); var keyObject = key.Compile()(data); PropertyInfo[] keyFields = keyObject.GetType().GetProperties().Select(p => t.GetProperty(p.Name)).ToArray(); if (keyFields == null) { throw new Exception($"{t.FullName} does not have a KeyAttribute field. Unable to exec AddOrUpdate call."); } var keyVals = keyFields.Select(p => p.GetValue(data)); var entities = dbSet.AsNoTracking().ToList(); int i = 0; foreach (var keyVal in keyVals) { // ReSharper disable once PossibleNullReferenceException entities = entities.Where(p => p.GetType().GetProperty(keyFields[i].Name).GetValue(p).Equals(keyVal)).ToList(); i++; } if (entities.Any()) { var dbVal = entities.FirstOrDefault(); var keyAttrs = data.GetType().GetProperties().Where(p => ids.Contains(p.Name)).ToList(); if (keyAttrs.Any()) { foreach (var keyAttr in keyAttrs) { keyAttr.SetValue(data, // ReSharper disable once PossibleNullReferenceException dbVal?.GetType() .GetProperties() .FirstOrDefault(p => p.Name == keyAttr.Name) .GetValue(dbVal)); } context.Entry(dbVal !).CurrentValues.SetValues(data); context.Entry(dbVal).State = EntityState.Modified; return; } } dbSet.Add(data); }
public async Task <List <Shift> > UpdateShifts(List <Shift> shifts) { var overlaps = await GetShiftConflicts(shifts); if (overlaps.Any()) { throw new BusinessLayerException(overlaps.SelectMany(ol => ol.ConflictMessages).ToStringWithPipes()); } var shiftIds = shifts.SelectToList(s => s.Id); var savedShifts = Db.Shift.Where(s => shiftIds.Contains(s.Id)); foreach (var shift in shifts) { var savedShift = savedShifts.FirstOrDefault(s => s.Id == shift.Id); savedShift.ThrowBusinessExceptionIfNull($"{nameof(Shift)} with the id: {shift.Id} could not be found."); if (shift.StartDate > shift.EndDate) { throw new BusinessLayerException($"{nameof(Shift)} Start date cannot come after end date."); } shift.Timezone.GetTimezone().ThrowBusinessExceptionIfNull($"A valid {nameof(shift.Timezone)} needs to be included in the {nameof(Shift)}."); shift.IsOvertime = IsShiftOvertime(shift.StartDate, shift.EndDate, shift.Timezone, OvertimeHours); Db.Entry(savedShift !).CurrentValues.SetValues(shift); Db.Entry(savedShift).Property(x => x.LocationId).IsModified = false; Db.Entry(savedShift).Property(x => x.ExpiryDate).IsModified = false; savedShift.Sheriff = await Db.Sheriff.FindAsync(shift.SheriffId); savedShift.AnticipatedAssignment = await Db.Assignment.FindAsync(shift.AnticipatedAssignmentId); } await Db.SaveChangesAsync(); return(await savedShifts.ToListAsync()); }
public async Task <CategoryDTO> EditAsync(int id, CategoryDTO entity) { _context.Entry(await _context.Categories.FirstOrDefaultAsync(x => x.Id == id) !).CurrentValues .SetValues(entity); var category = await _context.Categories.FindAsync(id); var categoryDto = _mapper.Map <CategoryDTO>(category); return(categoryDto); }
public async Task <OrderDTO> EditAsync(int id, OrderDTO entity) { _context.Entry(await _context.Orders.FirstOrDefaultAsync(x => x.Id == id) !).CurrentValues .SetValues(entity); var order = await _context.Photos.FindAsync(id); var orderDto = _mapper.Map <OrderDTO>(order); return(orderDto); }
public async Task <PhotoDTO> EditAsync(int id, PhotoDTO entity) { _context.Entry(await _context.Photos.FirstOrDefaultAsync(x => x.Id == id) !).CurrentValues .SetValues(entity); var photo = await _context.Photos.FindAsync(id); var photoDto = _mapper.Map <PhotoDTO>(photo); return(photoDto); }
public async Task UpdateAsync(Domain.SpeechAggregate.Speech speech) { if (speech == null) { throw new ArgumentNullRepositoryException(nameof(speech)); } var existingSpeech = await _context.Speech .Include(b => b.MediaFileItems) .FirstOrDefaultAsync(b => b.Id == speech.Id); _context.Entry(existingSpeech ?? throw new NotFoundRepositoryException(nameof(existingSpeech))).CurrentValues.SetValues(speech); }
public async Task <Comment> EditAsync(int id, CommentDTO entity) { _context.Entry(await _context.Coments.FirstOrDefaultAsync(x => x.Id == id) !).CurrentValues .SetValues(entity); var comment = await _context.Coments.FindAsync(id); return(comment); }
public void UpdateWithoutTrack <TTModel>(object entity) where TTModel : class { var propertyName = GetPrimaryKeyName <TTModel>(); var id = GetValueByPropertyName <TTModel>(entity); //DbContext.Entry(DbContext.Set<TTModel>().FirstOrDefaultPropertyName(propertyName, id) ?? throw new InvalidOperationException()).Reload(); DbContext.Entry(DbContext.Set <TTModel>().FirstOrDefaultPropertyName(propertyName, id) ?? throw new InvalidOperationException()).CurrentValues.SetValues(entity); }
public async Task <ShopingCartItem> EditAsync(int id, ShopingCartItemDto entity) { _context.Entry(await _context.ShopingCart.FirstOrDefaultAsync(x => x.Id == id) !).CurrentValues .SetValues(entity); var comment = await _context.ShopingCart.FindAsync(id); return(comment); }