コード例 #1
0
        public async Task <ActionResult <DepartmentResource> > UpdateDepartment(long id, [FromBody] SaveDepartmentResource model)
        {
            var validator        = new SaveDepartmentResourceValidator();
            var validationResult = await validator.ValidateAsync(model);

            var requestIsInvalid = id == 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var depToBeUpdate = await _departmentService.GetDepartmentById(id);

            if (depToBeUpdate == null)
            {
                return(NotFound());
            }

            var department = _mapper.Map <SaveDepartmentResource, Department>(model);

            await _departmentService.UpdateDepartment(depToBeUpdate, department);

            var updatedDep = await _departmentService.GetDepartmentById(id);

            var updatedDepartmentResource = _mapper.Map <Department, DepartmentResource>(updatedDep);

            return(Ok(updatedDepartmentResource));
        }
コード例 #2
0
        public async Task <ActionResult <DepartmentResource> > UpdateDepartment(int id, [FromBody] SaveDepartmentResource saveDepartmentResource)
        {
            var validator        = new SaveDepartmentResourceValidator();
            var validationResult = await validator.ValidateAsync(saveDepartmentResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var departmentToBeUpdated = await departmentService.GetDepartmentById(id);

            if (departmentToBeUpdated == null)
            {
                return(NotFound());
            }

            var department = mapper.Map <SaveDepartmentResource, Department>(saveDepartmentResource);

            await departmentService.UpdateDepartment(departmentToBeUpdated, department);

            var updatedDepartment = await departmentService.GetDepartmentById(id);

            var updatedDepartmentResource = mapper.Map <Department, DepartmentResource>(updatedDepartment);

            return(Ok(updatedDepartmentResource));
        }
コード例 #3
0
        public async Task <ActionResult <DepartmentResource> > CreateDepartment([FromBody] SaveDepartmentResource saveDepartmentResource)
        {
            var validator        = new SaveDepartmentResourceValidator();
            var validationResult = await validator.ValidateAsync(saveDepartmentResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var departmentToCreate = mapper.Map <SaveDepartmentResource, Department>(saveDepartmentResource);

            var newDepartment = await departmentService.CreateDepartment(departmentToCreate);

            var department = await departmentService.GetDepartmentById(newDepartment.Id);

            var departmentResource = mapper.Map <Department, DepartmentResource>(department);

            return(Ok(departmentResource));
        }
コード例 #4
0
        public async Task <ActionResult <DepartmentResource> > CreateDepartment([FromBody] SaveDepartmentResource model)
        {
            var validator        = new SaveDepartmentResourceValidator();
            var validationResult = await validator.ValidateAsync(model);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var departmentToCreate = _mapper.Map <SaveDepartmentResource, Department>(model);

            var newDepartment = await _departmentService.CreateDepartment(departmentToCreate);

            var department = await _departmentService.GetDepartmentById(newDepartment.Id);

            var departmentResource = _mapper.Map <Department, DepartmentResource>(department);

            return(Ok(departmentResource));
        }