96 lines
2.9 KiB
C#
96 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Tasks;
|
|
using api.Data;
|
|
using api.Dtos.Stock;
|
|
using api.Interfaces;
|
|
using api.Mappers;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace api.Controllers
|
|
{
|
|
[Route("api/stock")]
|
|
[ApiController]
|
|
public class StockController : ControllerBase
|
|
{
|
|
private readonly IStockRepository _stockRepo;
|
|
private readonly ApplicationDbContext _context;
|
|
public StockController(ApplicationDbContext context, IStockRepository stockRepo)
|
|
{
|
|
_stockRepo = stockRepo;
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAll()
|
|
{
|
|
var stocks = await _stockRepo.GetAllAsync();
|
|
var stockDto = stocks.Select(s => s.ToStockDto()).ToList();
|
|
return Ok(stockDto);
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<IActionResult> GetById([FromRoute] int id)
|
|
{
|
|
var stock = await _context.Stocks.FindAsync(id);
|
|
if (stock == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(stock.ToStockDto());
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create([FromBody] CreateStockRequestDto stockDto)
|
|
{
|
|
var stockModel = stockDto.ToStockModel();
|
|
await _context.Stocks.AddAsync(stockModel);
|
|
await _context.SaveChangesAsync();
|
|
return CreatedAtAction(nameof(GetById), new { id = stockModel.Id }, stockModel.ToStockDto());
|
|
}
|
|
|
|
|
|
[HttpPut]
|
|
[Route("{id}")]
|
|
|
|
public async Task<IActionResult> Update([FromRoute] int id, [FromBody] UpdateStockRequestDto updateDto)
|
|
{
|
|
var stockModel = await _context.Stocks.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
if (stockModel == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
stockModel.Symbol = updateDto.Symbol;
|
|
stockModel.CompanyName = updateDto.CompanyName;
|
|
stockModel.Purchase = updateDto.Purchase;
|
|
stockModel.LastDiv = updateDto.LastDiv;
|
|
stockModel.Industry = updateDto.Industry;
|
|
stockModel.MarketCap = updateDto.MarketCap;
|
|
|
|
await _context.SaveChangesAsync();
|
|
return Ok(stockModel.ToStockDto());
|
|
}
|
|
|
|
[HttpDelete]
|
|
[Route("{id}")]
|
|
public async Task<IActionResult> Delete([FromRoute] int id)
|
|
{
|
|
var stockModel = await _context.Stocks.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
if (stockModel == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.Stocks.Remove(stockModel);
|
|
await _context.SaveChangesAsync();
|
|
return NoContent();
|
|
}
|
|
|
|
}
|
|
} |