コード例 #1
0
        public async Task <ActionResult <ColorResource> > UpdateColor(int id, [FromBody] SaveColorResource saveColorResource)
        {
            var validator        = new SaveColorResourceValidator();
            var validationResult = await validator.ValidateAsync(saveColorResource);

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

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

            var color = _mapper.Map <SaveColorResource, Color>(saveColorResource);

            await _colorService.UpdateColor(colorToBeUpdated, color);

            var updatedColor = await _colorService.GetColorById(id);

            var updatedColorResource = _mapper.Map <Color, ColorResource>(updatedColor);

            return(Ok(updatedColorResource));
        }
コード例 #2
0
        public async Task <ActionResult <ColorResource> > CreateColor([FromBody] SaveColorResource saveColorResource)
        {
            var validator        = new SaveColorResourceValidator();
            var validationResult = await validator.ValidateAsync(saveColorResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var colorToCreate = _mapper.Map <SaveColorResource, Color>(saveColorResource);

            var newColor = await _colorService.CreateColor(colorToCreate);

            var color = await _colorService.GetColorById(newColor.Id);

            var colorResource = _mapper.Map <Color, ColorResource>(color);

            return(Ok(colorResource));
        }