Ejemplo n.º 1
0
        /// <summary>
        /// Checks Name
        /// </summary>
        /// <param name="viewModel">Injected <see cref="UpdateYear"/></param>
        /// <returns>Instance of <see cref="Task{Year}"/></returns>
        public async Task <Year> CheckNumber(UpdateYear @viewModel)
        {
            Year @Year = await Context.Year
                         .TagWith("CheckNumber")
                         .AsNoTracking()
                         .FirstOrDefaultAsync(x => x.Number == @viewModel.Number && x.Id != viewModel.Id);

            if (@Year != null)
            {
                // Log
                string @logData = @Year.GetType().Name
                                  + " with Number "
                                  + @Year.Number
                                  + " was already found at "
                                  + DateTime.Now.ToShortTimeString();

                Logger.WriteGetItemFoundLog(@logData);

                throw new Exception(@Year.GetType().Name
                                    + " with Number "
                                    + @viewModel.Number
                                    + " already exists");
            }

            return(Year);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates Year
        /// </summary>
        /// <param name="viewModel">Injected <see cref="UpdateYear"/></param>
        /// <returns>Instance of <see cref="Task{ViewYear}"/></returns>
        public async Task <ViewYear> UpdateYear(UpdateYear @viewModel)
        {
            await CheckNumber(@viewModel);

            Year @Year = await FindYearById(@viewModel.Id);

            @Year.Number = @viewModel.Number;

            try
            {
                Context.Year.Update(@Year);

                await Context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                await CheckNumber(@viewModel);
            }

            // Log
            string @logData = @Year.GetType().Name
                              + " with Id "
                              + @Year.Id
                              + " was modified at "
                              + DateTime.Now.ToShortTimeString();

            Logger.WriteUpdateItemLog(@logData);

            return(Mapper.Map <ViewYear>(@Year));
        }