Ejemplo n.º 1
0
        /// <summary>
        /// Updates Grade
        /// </summary>
        /// <param name="viewModel">Injected <see cref="UpdateGrade"/></param>
        /// <returns>Instance of <see cref="Task{ViewGrade}"/></returns>
        public async Task <ViewGrade> UpdateGrade(UpdateGrade @viewModel)
        {
            await CheckName(@viewModel);

            Grade @Grade = await FindGradeById(@viewModel.Id);

            @Grade.Name     = @viewModel.Name;
            @Grade.ImageUri = @viewModel.ImageUri;

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

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

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

            Logger.WriteUpdateItemLog(@logData);

            return(Mapper.Map <ViewGrade>(@Grade));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks Name
        /// </summary>
        /// <param name="viewModel">Injected <see cref="UpdateGrade"/></param>
        /// <returns>Instance of <see cref="Task{Grade}"/></returns>
        public async Task <Grade> CheckName(UpdateGrade @viewModel)
        {
            Grade @Grade = await Context.Grade
                           .TagWith("CheckName")
                           .AsNoTracking()
                           .FirstOrDefaultAsync(x => x.Name == @viewModel.Name && x.Id != viewModel.Id);

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

                Logger.WriteGetItemFoundLog(@logData);

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

            return(Grade);
        }
Ejemplo n.º 3
0
        public void UpdateStudentGrade_SmallerThan0_Error()
        {
            var enrollmentId = 45;
            var obj          = new UpdateGrade
            {
                Grade = -1
            };
            var ex = Assert.ThrowsAsync <ArgumentException>(async() => await _enrollmentLogic.UpdateStudentGrade(enrollmentId, obj));

            Assert.That(ex.Message, Is.EqualTo("Grade must be between 0 and 100"));
        }
Ejemplo n.º 4
0
        public void UpdateStudentGrade_InvalidId_Error()
        {
            var enrollmentId = 999;
            var obj          = new UpdateGrade
            {
                Grade = (decimal?)27.8
            };
            var ex = Assert.ThrowsAsync <ArgumentException>(async() => await _enrollmentLogic.UpdateStudentGrade(enrollmentId, obj));

            Assert.That(ex.Message, Is.EqualTo($"There's no enrollment with enrollmentId = {enrollmentId}"));
        }
Ejemplo n.º 5
0
        public async Task UpdateStudentGrade_NullGrade_Ok()
        {
            var enrollmentId = 45;
            var obj          = new UpdateGrade
            {
                Grade = null
            };
            await _enrollmentLogic.UpdateStudentGrade(enrollmentId, obj);

            var actual = await _enrollmentRepository.GetById(enrollmentId);

            Assert.AreEqual(actual.Grade, obj.Grade);
        }
        public async Task <IActionResult> UpdateGrade(int enrollmentId, [FromBody] UpdateGrade updateGrade)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(ValidationProblem("Invalid parameters"));
                }

                await _enrollmentLogic.UpdateStudentGrade(enrollmentId, updateGrade);

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 7
0
        public async Task UpdateStudentGrade(int id, UpdateGrade updateGrade)
        {
            if (updateGrade.Grade.HasValue && (updateGrade.Grade.Value < 0 || updateGrade.Grade.Value > 100))
            {
                throw new ArgumentException("Grade must be between 0 and 100");
            }

            if (await _enrollmentRepository.GetById(id) == null)
            {
                throw new ArgumentException($"There's no enrollment with enrollmentId = {id}");
            }

            var enrollment = new Enrollment
            {
                EnrollmentId = id,
                Grade        = updateGrade.Grade
            };

            await _enrollmentRepository.Update(id, enrollment);
        }