예제 #1
0
        public async Task CreateAsync(Portfolio portfolio)
        {
            var @portfolioResult = await _portfolioRepository.GetAsync(portfolio.ISINCode, portfolio.Date);

            if (@portfolioResult != null)
            {
                throw new Exception($"Portfolio ISIN: {portfolio.ISINCode} already exists.");
            }

            await _portfolioRepository.AddAsync(portfolio);
        }
        public async Task <IActionResult> PostPortfolioAsync([FromBody] Portfolio portfolio, [FromRoute] int id)
        {
            Portfolio created;

            try {
                var user = await _userRepository.GetAsync(id);

                created = await _portfolioRepository.AddAsync(portfolio, user);
            } catch (ArgumentException e) {
                _logger.LogInformation(e, "Attempted to add a portfolio with an already existing Id.");
                return(BadRequest(e.Message));
            } catch (DbUpdateException e) {
                _logger.LogInformation(e, "Attempted to add a portfolio that violated database constraints.");
                return(BadRequest(e.Message));
            }

            _logger.LogInformation("Successfully created portfolio.", created);
            return(CreatedAtAction(nameof(PortfoliosController.GetByIdAsync), "Portfolios", new { id = created.Id }, created));
        }