From b4f4860853404a9198b27893bbeda365dc86ffdf Mon Sep 17 00:00:00 2001 From: Jose E Rodriguez-Marrero Date: Tue, 25 Jun 2024 17:40:01 -0700 Subject: [PATCH] Add stock to portfolio --- api/Controllers/PortfolioController.cs | 28 ++++++++++++++++++++++++++ api/Interfaces/IPortfolioRepo.cs | 1 + api/Interfaces/IStockRepo.cs | 1 + api/Mappers/StockMappers.cs | 6 +++--- api/Repositories/PortfolioRepo.cs | 26 +++++++++++++++++++++++- api/Repositories/StockRepo.cs | 12 +++++++++++ 6 files changed, 70 insertions(+), 4 deletions(-) diff --git a/api/Controllers/PortfolioController.cs b/api/Controllers/PortfolioController.cs index 009d747..9266623 100644 --- a/api/Controllers/PortfolioController.cs +++ b/api/Controllers/PortfolioController.cs @@ -43,5 +43,33 @@ public async Task GetUserPortfolio() return Ok(userPortfolio); } + + [HttpPost] + [Authorize] + public async Task AddStockToPortfolio(string symbol) + { + var username = User.GetUsername(); + + if (username == null) + { + return Unauthorized(); + } + + var appUser = await _userManager.FindByNameAsync(username); + + if (appUser == null) + { + return Unauthorized(); + } + + var stockDto = await _portfolioRepo.AddStockToPortfolioAsync(appUser, symbol); + + if (stockDto == null) + { + return NotFound(); + } + + return CreatedAtAction(nameof(GetUserPortfolio), new { id = stockDto.Id }, stockDto); + } } } \ No newline at end of file diff --git a/api/Interfaces/IPortfolioRepo.cs b/api/Interfaces/IPortfolioRepo.cs index db4bb38..63bab5e 100644 --- a/api/Interfaces/IPortfolioRepo.cs +++ b/api/Interfaces/IPortfolioRepo.cs @@ -6,5 +6,6 @@ namespace api.Interfaces public interface IPortfolioRepo { Task> GetUserPortfolioAsync(AppUser user); + Task AddStockToPortfolioAsync(AppUser user, string symbol); } } \ No newline at end of file diff --git a/api/Interfaces/IStockRepo.cs b/api/Interfaces/IStockRepo.cs index 1213b79..52f8a7f 100644 --- a/api/Interfaces/IStockRepo.cs +++ b/api/Interfaces/IStockRepo.cs @@ -8,6 +8,7 @@ public interface IStockRepo { Task> GetAllAsync(QueryObject query); Task GetByIdAsync(int id); + Task GetBySymbolAsync(string symbol); Task CreateAsync(Stock stockModel); Task UpdateAsync(int id, UpdateStockRequestDto stockDto); Task DeleteAsync(int id); diff --git a/api/Mappers/StockMappers.cs b/api/Mappers/StockMappers.cs index 17a7b62..121d9cb 100644 --- a/api/Mappers/StockMappers.cs +++ b/api/Mappers/StockMappers.cs @@ -24,11 +24,11 @@ public static Stock ToStockFromCreateDto(this CreateStockRequestDto stockDto) { return new Stock { - Symbol = stockDto.Symbol, - CompanyName = stockDto.CompanyName, + Symbol = stockDto.Symbol.ToLower(), + CompanyName = stockDto.CompanyName.ToLower(), Purchase = stockDto.Purchase, LastDiv = stockDto.LastDiv, - Industry = stockDto.Industry, + Industry = stockDto.Industry.ToLower(), MarketCap = stockDto.MarketCap }; } diff --git a/api/Repositories/PortfolioRepo.cs b/api/Repositories/PortfolioRepo.cs index f453031..6f9f265 100644 --- a/api/Repositories/PortfolioRepo.cs +++ b/api/Repositories/PortfolioRepo.cs @@ -10,16 +10,40 @@ namespace api.Repositories public class PortfolioRepo : IPortfolioRepo { private readonly AppDbContext _context; + private readonly IStockRepo _stockRepo; - public PortfolioRepo(AppDbContext context) + public PortfolioRepo(AppDbContext context, IStockRepo stockRepo) { _context = context; + _stockRepo = stockRepo; } + public async Task> GetUserPortfolioAsync(AppUser user) { return await _context.Portfolios.Where(x => x.AppUserId == user.Id) .Select(stock => stock.Stock.ToStockDto()) .ToListAsync(); } + + public async Task AddStockToPortfolioAsync(AppUser user, string symbol) + { + var stock = await _stockRepo.GetBySymbolAsync(symbol); + + if (stock == null) + { + return null; + } + + var portfolio = new Portfolio + { + AppUserId = user.Id, + StockId = stock.Id + }; + + await _context.Portfolios.AddAsync(portfolio); + await _context.SaveChangesAsync(); + + return stock.ToStockDto(); + } } } \ No newline at end of file diff --git a/api/Repositories/StockRepo.cs b/api/Repositories/StockRepo.cs index f88ea7a..3141036 100644 --- a/api/Repositories/StockRepo.cs +++ b/api/Repositories/StockRepo.cs @@ -87,6 +87,18 @@ private IQueryable PaginateStocks(IQueryable stocks, QueryObject q return stockModel; } + public async Task GetBySymbolAsync(string symbol) + { + var stockModel = await _context.Stocks.Include(c => c.Comments).FirstOrDefaultAsync(s => s.Symbol == symbol); + + if (stockModel is null) + { + return null; + } + + return stockModel; + } + public async Task UpdateAsync(int id, UpdateStockRequestDto stockDto) { var stock = await _context.Stocks.FirstOrDefaultAsync(x => x.Id == id);