Пример #1
0
        public async Task <ActionResult> CreateItemAsync([FromBody] UserRoleDTO userRoleDTO, CancellationToken cancellationToken)
        {
            var newItem = _mapper.Map <UserRole>(userRoleDTO);

            newItem = await _userRoleService.AddAsync(newItem, cancellationToken);

            if (newItem == null)
            {
                AssignToModelState(_userRoleService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(ItemByIdAsync), new { id = newItem.Id }, null));
        }
Пример #2
0
        public async Task <ActionResult> UpdateItemAsync([FromBody] UserRoleDTO userRoleDTO, CancellationToken cancellationToken)
        {
            var specFilter = new UserRoleFilterSpecification(userRoleDTO.Id.Value);
            var rowCount   = await _userRoleService.CountAsync(specFilter, cancellationToken);

            if (rowCount == 0)
            {
                throw new EntityNotFoundException(nameof(UserRole), userRoleDTO.Id);
            }

            // bind to old item
            var item = _mapper.Map <UserRole>(userRoleDTO);

            var result = await _userRoleService.UpdateAsync(item, cancellationToken);

            if (!result)
            {
                AssignToModelState(_userRoleService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(ItemByIdAsync), new { id = item.Id }, null));
        }