Beispiel #1
0
        public async Task <ActionResult <ModelRes> > Update(int id, [FromBody] SaveModelRes saveModelRes)
        {
            var validator        = new SaveModelValidator();
            var validationResult = await validator.ValidateAsync(saveModelRes);

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

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

            var modelToBeUpdate = await _service.GetById(id);

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

            var model = _mapper.Map <SaveModelRes, Model>(saveModelRes);

            await _service.Update(modelToBeUpdate, model);

            var updatedModel = await _service.GetById(id);

            var updatedModelResource = _mapper.Map <Model, ModelRes>(updatedModel);

            return(Ok(updatedModelResource));
        }
Beispiel #2
0
        public async Task <ActionResult <ModelRes> > Create([FromBody] SaveModelRes saveModelRes)
        {
            var validator        = new SaveModelValidator();
            var validationResult = await validator.ValidateAsync(saveModelRes);

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

            var create = await _service.Create(_mapper.Map <SaveModelRes, Model>(saveModelRes));

            var model = await _service.GetById(create.ModelId);

            var resource = _mapper.Map <Model, ModelRes>(model);

            return(Ok(resource));
        }