/// <summary> /// Finds Bandera By Id /// </summary> /// <param name="id">Injected <see cref="int"/></param> /// <returns>Instance of <see cref="Task{Bandera}"/></returns> public async Task <Bandera> FindBanderaById(int @id) { Bandera @bandera = await Context.Bandera .TagWith("FindBanderaById") .FirstOrDefaultAsync(x => x.Id == @id); if (@bandera == null) { // Log string @logData = @bandera.GetType().Name + " with Id " + @id + " was not found at " + DateTime.Now.ToShortTimeString(); Logger.WriteGetItemNotFoundLog(@logData); throw new Exception(@bandera.GetType().Name + " with Id " + @id + " does not exist"); } return(@bandera); }
/// <summary> /// Checks Name /// </summary> /// <param name="viewModel">Injected <see cref="UpdateBandera"/></param> /// <returns>Instance of <see cref="Task{Bandera}"/></returns> public async Task <Bandera> CheckName(UpdateBandera @viewModel) { Bandera @bandera = await Context.Bandera .TagWith("CheckName") .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == @viewModel.Name && x.Id != viewModel.Id); if (@bandera != null) { // Log string @logData = @bandera.GetType().Name + " with Name " + @bandera.Name + " was already found at " + DateTime.Now.ToShortTimeString(); Logger.WriteGetItemFoundLog(@logData); throw new Exception(@bandera.GetType().Name + " with Name " + @viewModel.Name + " already exists"); } return(bandera); }
/// <summary> /// Updates Bandera /// </summary> /// <param name="viewModel">Injected <see cref="UpdateBandera"/></param> /// <returns>Instance of <see cref="Task{ViewBandera}"/></returns> public async Task <ViewBandera> UpdateBandera(UpdateBandera @viewModel) { await CheckName(@viewModel); Bandera @bandera = await FindBanderaById(@viewModel.Id); @bandera.Name = @viewModel.Name; @bandera.ImageUri = @viewModel.ImageUri; try { Context.Bandera.Update(@bandera); await Context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { await CheckName(@viewModel); } // Log string @logData = @bandera.GetType().Name + " with Id " + @bandera.Id + " was modified at " + DateTime.Now.ToShortTimeString(); Logger.WriteUpdateItemLog(@logData); return(Mapper.Map <ViewBandera>(@bandera)); }
/// <summary> /// Removes Bandera By Id /// </summary> /// <param name="id">Injected <see cref="int"/></param> /// <returns>Instance of <see cref="Task"/></returns> public async Task RemoveBanderaById(int @id) { try { Bandera @bandera = await FindBanderaById(@id); Context.Bandera.Remove(@bandera); await Context.SaveChangesAsync(); // Log string @logData = @bandera.GetType().Name + " with Id " + @bandera.Id + " was removed at " + DateTime.Now.ToShortTimeString(); Logger.WriteDeleteItemLog(@logData); } catch (DbUpdateConcurrencyException) { await FindBanderaById(@id); } }