/// <summary> /// Should only be used on creating shares, not when transfering them /// </summary> /// <param name="portfolioId"></param> /// <param name="sharesViewModel"></param> /// <returns></returns> public async Task <Share> Create(Guid portfolioId, CreateSharesViewModel sharesViewModel) { Portfolio portfolio = await _unitOfWork.PortfolioRepository.GetAsync(portfolioId); if (portfolio == null) { throw new ArgumentException("Portfolio not found"); } Share share; try { var shareSot = await CreateShare(sharesViewModel.Name, sharesViewModel.TotalCount, sharesViewModel.TotalValue); share = new Share { Portfolio = portfolio, ShareId = shareSot.ShareId, Count = shareSot.TotalCount }; portfolio.Shares.Add(share); } catch (Exception e) { throw new ArgumentException("Share couldn't be created"); } _unitOfWork.SharesRepository.Add(share); _unitOfWork.PortfolioRepository.Update(portfolio); await _unitOfWork.CommitAsync(); return(share); }
public async Task <IActionResult> AddShare([FromRoute] Guid portfolioId, [FromBody] CreateSharesViewModel sharesViewModel) { if (!ModelState.IsValid) { return(BadRequest()); } var share = await _sharesService.Create(portfolioId, sharesViewModel); return(CreatedAtAction(nameof(GetShareByPortfolio), new { share.Portfolio.PortfolioId, share.ShareId }, share)); }