Esempio n. 1
0
        public async Task <ActionResult <RoleDTO> > UpdateRole(int id, [FromBody] CreateRoleDTO saveRoleResource)
        {
            var validator        = new CreateRoleResourceValidator();
            var validationResult = await validator.ValidateAsync(saveRoleResource);

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

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

            var role = _mapper.Map <CreateRoleDTO, Role>(saveRoleResource);

            await _roleService.UpdateRole(roleToBeUpdated, role);

            var updatedRole = await _roleService.GetRoleById(id);

            var updatedRoleResource = _mapper.Map <Role, RoleDTO>(updatedRole);

            return(Ok(updatedRoleResource));
        }
Esempio n. 2
0
        public async Task <ActionResult <RoleDTO> > CreateRole([FromBody] CreateRoleDTO createRoleResource)
        {
            var validator        = new CreateRoleResourceValidator();
            var validationResult = await validator.ValidateAsync(createRoleResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var roleToCreate = _mapper.Map <CreateRoleDTO, Role>(createRoleResource);

            var newRole = await _roleService.CreateRole(roleToCreate);

            var role = await _roleService.GetRoleById(newRole.Id);

            var roleResource = _mapper.Map <Role, RoleDTO>(role);

            return(Ok(roleResource));
        }