/// <summary> /// Checks Name /// </summary> /// <param name="viewModel">Injected <see cref="UpdateArenal"/></param> /// <returns>Instance of <see cref="Task{Arenal}"/></returns> public async Task <Arenal> CheckName(UpdateArenal @viewModel) { Arenal @arenal = await Context.Arenal .AsNoTracking() .TagWith("CheckName") .FirstOrDefaultAsync(x => x.Name == @viewModel.Name && x.Id != @viewModel.Id); if (@arenal != null) { // Log string @logData = @arenal.GetType().Name + " with Name " + @arenal.Name + " was already found at " + DateTime.Now.ToShortTimeString(); Logger.WriteGetItemFoundLog(@logData); throw new Exception(@arenal.GetType().Name + " with Name " + @viewModel.Name + " already exists"); } return(@arenal); }
/// <summary> /// Finds Arenal By Id /// </summary> /// <param name="id">Injected <see cref="int"/></param> /// <returns>Instance of <see cref="Task{Arenal}"/></returns> public async Task <Arenal> FindArenalById(int @id) { Arenal @arenal = await Context.Arenal .TagWith("FindArenalById") .AsQueryable() .AsSplitQuery() .Include(x => x.ArenalPoblaciones) .ThenInclude(x => x.Poblacion) .FirstOrDefaultAsync(x => x.Id == @id); if (@arenal == null) { // Log string @logData = @arenal.GetType().Name + " with Id " + @id + " was not found at " + DateTime.Now.ToShortTimeString(); Logger.WriteGetItemNotFoundLog(@logData); throw new Exception(@arenal.GetType().Name + " with Id " + @id + " does not exist"); } return(@arenal); }
/// <summary> /// Updates Arenal /// </summary> /// <param name="viewModel">Injected <see cref="UpdateArenal"/></param> /// <returns>Instance of <see cref="Task{ViewArenal}"/></returns> public async Task <ViewArenal> UpdateArenal(UpdateArenal @viewModel) { await CheckName(@viewModel); Arenal @arenal = await FindArenalById(@viewModel.Id); @arenal.Name = @viewModel.Name; @arenal.ArenalPoblaciones = new List <ArenalPoblacion>(); @arenal.Historicos = new List <Historico>(); try { Context.Arenal.Update(@arenal); UpdateArenalPoblacion(@viewModel, @arenal); await UpdateHistorico(@arenal); await Context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { await CheckName(@viewModel); } // Log string @logData = @arenal.GetType().Name + " with Id" + @arenal.Id + " was modified at " + DateTime.Now.ToShortTimeString(); Logger.WriteUpdateItemLog(@logData); return(Mapper.Map <ViewArenal>(@arenal));; }
/// <summary> /// Removes Arenal By Id /// </summary> /// <param name="id">Injected <see cref="int"/></param> /// <returns>Instance of <see cref="Task"/></returns> public async Task RemoveArenalById(int @id) { try { Arenal @arenal = await FindArenalById(@id); Context.Arenal.Remove(@arenal); await Context.SaveChangesAsync(); // Log string @logData = @arenal.GetType().Name + " with Id" + @arenal.Id + " was removed at " + DateTime.Now.ToShortTimeString(); Logger.WriteDeleteItemLog(@logData); } catch (DbUpdateConcurrencyException) { await FindArenalById(@id); } }