public async Task <IActionResult> PutEnrollment([FromRoute] int id, [FromBody] Enrollment enrollment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != enrollment.Id)
            {
                return(BadRequest());
            }

            _context.Entry(enrollment).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EnrollmentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 2
0
        public async Task <Unit> Handle(UpdateDepartmentCommand request, CancellationToken cancellationToken)
        {
            if (request.DepartmentID == null)
            {
                throw new NotFoundException(nameof(Department), request.DepartmentID);
            }

            var departmentToUpdate = await _context.Departments
                                     .Include(i => i.Administrator)
                                     .FirstOrDefaultAsync(m => m.DepartmentID == request.DepartmentID);

            if (departmentToUpdate == null)
            {
                throw new NotFoundException(nameof(Department), request.DepartmentID);
            }

            departmentToUpdate.Name         = request.Name;
            departmentToUpdate.Budget       = request.Budget;
            departmentToUpdate.StartDate    = request.StartDate;
            departmentToUpdate.InstructorID = request.InstructorID;

            _context.Entry <Department>(departmentToUpdate).Property("RowVersion").OriginalValue = request.RowVersion;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }