示例#1
0
        public async Task <ActionResult <ModelRes> > Update(int id, [FromBody] SavePortfolioRes savePortfolioRes)
        {
            var validator        = new SavePortfolioValidator();
            var validationResult = await validator.ValidateAsync(savePortfolioRes);

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

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

            var portfolioToBeUpdate = await _service.GetById(id);

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

            var portfolio = _mapper.Map <SavePortfolioRes, Portfolio>(savePortfolioRes);

            await _service.Update(portfolioToBeUpdate, portfolio);

            var updatedPortfolio = await _service.GetById(id);

            var updatedPortfolioResource = _mapper.Map <Portfolio, PortfolioRes>(updatedPortfolio);

            return(Ok(updatedPortfolioResource));
        }
示例#2
0
        public async Task <ActionResult <PortfolioRes> > Create([FromBody] SavePortfolioRes savePortfolioRes)
        {
            var validator        = new SavePortfolioValidator();
            var validationResult = await validator.ValidateAsync(savePortfolioRes);

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

            var create = await _service.Create(_mapper.Map <SavePortfolioRes, Portfolio>(savePortfolioRes));

            var portfolio = await _service.GetById(create.UserId);

            var resource = _mapper.Map <Portfolio, PortfolioRes>(portfolio);

            return(Ok(resource));
        }