Skip to content

Commit

Permalink
Add stock to portfolio
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorNull committed Jun 26, 2024
1 parent 76ba50c commit b4f4860
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
28 changes: 28 additions & 0 deletions api/Controllers/PortfolioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,33 @@ public async Task<IActionResult> GetUserPortfolio()

return Ok(userPortfolio);
}

[HttpPost]
[Authorize]
public async Task<IActionResult> 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);
}
}
}
1 change: 1 addition & 0 deletions api/Interfaces/IPortfolioRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace api.Interfaces
public interface IPortfolioRepo
{
Task<List<StockDto>> GetUserPortfolioAsync(AppUser user);
Task<StockDto?> AddStockToPortfolioAsync(AppUser user, string symbol);
}
}
1 change: 1 addition & 0 deletions api/Interfaces/IStockRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface IStockRepo
{
Task<List<Stock>> GetAllAsync(QueryObject query);
Task<Stock?> GetByIdAsync(int id);
Task<Stock?> GetBySymbolAsync(string symbol);
Task<Stock> CreateAsync(Stock stockModel);
Task<Stock?> UpdateAsync(int id, UpdateStockRequestDto stockDto);
Task<Stock?> DeleteAsync(int id);
Expand Down
6 changes: 3 additions & 3 deletions api/Mappers/StockMappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
Expand Down
26 changes: 25 additions & 1 deletion api/Repositories/PortfolioRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<StockDto>> GetUserPortfolioAsync(AppUser user)
{
return await _context.Portfolios.Where(x => x.AppUserId == user.Id)
.Select(stock => stock.Stock.ToStockDto())
.ToListAsync();
}

public async Task<StockDto?> 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();
}
}
}
12 changes: 12 additions & 0 deletions api/Repositories/StockRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ private IQueryable<Stock> PaginateStocks(IQueryable<Stock> stocks, QueryObject q
return stockModel;
}

public async Task<Stock?> 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<Stock?> UpdateAsync(int id, UpdateStockRequestDto stockDto)
{
var stock = await _context.Stocks.FirstOrDefaultAsync(x => x.Id == id);
Expand Down

0 comments on commit b4f4860

Please sign in to comment.