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