Skip to content

Commit

Permalink
Add Price Service
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimeTimeTran committed Jun 2, 2023
1 parent 1801fbe commit e50eeb7
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 34 deletions.
93 changes: 93 additions & 0 deletions API/Controllers/PriceController.cs
@@ -0,0 +1,93 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Common.Models;
using Services.Interfaces;
using System;

namespace API.Controllers
{
[Authorize]
[ApiController]
[Route("prices")]
public class PriceController : ControllerBase
{
private readonly ILogger<PriceController> _logger;
private readonly IPriceService _priceService;

public PriceController(ILogger<PriceController> logger, IPriceService priceService)
{
_logger = logger;
_priceService = priceService;
}

[HttpGet]
public IActionResult GetAll()
{
try
{
var prices = _priceService.GetAll();
return Ok(prices);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString()); // Output the exception details
return StatusCode(500, "An error occurred while fetching prices.");
}
}

[HttpGet("{id}")]
public IActionResult GetById(int id)
{
try
{
return Ok(_priceService.GetPriceById(id));
}
catch (KeyNotFoundException)
{
return NotFound();
}
}

[HttpPost]
public IActionResult Create([FromBody] PriceDTO dto)
{
try
{
PriceDTO price = _priceService.Add(dto);
return CreatedAtAction(nameof(GetById), new { id = price.Id }, price);
}
catch (Exception ex)
{
return StatusCode(500, "An error occurred while creating the price.");
}
}

[HttpPut("{id}")]
public IActionResult Update(int id, [FromBody] PriceDTO dto)
{
try
{
PriceDTO price = _priceService.Update(id, dto);
return Ok(price);
}
catch (KeyNotFoundException)
{
return NotFound();
}
}

[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
try
{
_priceService.Delete(id);
return NoContent();
}
catch (KeyNotFoundException)
{
return NotFound();
}
}
}
}
3 changes: 3 additions & 0 deletions API/Program.cs
Expand Up @@ -35,12 +35,15 @@
});

builder.Services.AddScoped<IStockService, StockService>();
builder.Services.AddScoped<IPriceService, PriceService>();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();

var mapperConfig = new MapperConfiguration(config =>
{
config.CreateMap<Stock, StockDTO>();
config.CreateMap<StockDTO, Stock>();
config.CreateMap<Price, PriceDTO>();
config.CreateMap<PriceDTO, Price>();
});

// Create an instance of IMapper using the configured mapper configuration
Expand Down
36 changes: 36 additions & 0 deletions Common/Models/PriceDTO.cs
@@ -0,0 +1,36 @@
namespace Common.Models;

public class PriceDTO
{
public int? Id { get; set; }
public int? StockId { get; set; }
public int? TransactionCount { get; set; }
public DateTime? DateOfAggregation { get; set; }
public decimal? o { get; set; } // open
public decimal? l { get; set; } // low
public decimal? h { get; set; } // high
public decimal? c { get; set; } // close
public decimal? v { get; set; } // volume
public decimal? vwa { get; set; } // volume weighted average

// Parameterless constructor
public PriceDTO()
{
}

public PriceDTO(int? id, int? stockId, int? transactionCount, DateTime? dateOfAggregation,
decimal? open, decimal? low, decimal? high, decimal? close,
decimal? volume, decimal? volumeWeightedAverage)
{
Id = id;
StockId = stockId;
TransactionCount = transactionCount;
DateOfAggregation = dateOfAggregation;
o = open;
l = low;
h = high;
c = close;
v = volume;
vwa = volumeWeightedAverage;
}
}
1 change: 1 addition & 0 deletions DataAccess/Context/RSEContext.cs
Expand Up @@ -6,6 +6,7 @@ namespace DataAccess.Context
public class RseContext : DbContext
{
public DbSet<Stock> Stocks { get; set; }
public DbSet<Price> Prices { get; set; }
// Add DbSet properties for other entities

public RseContext(DbContextOptions<RseContext> options) : base(options)
Expand Down
16 changes: 16 additions & 0 deletions DataAccess/Entities/Price.cs
@@ -0,0 +1,16 @@
using System;

namespace DataAccess.Entities;
public class Price
{
public int Id { get; set; }
public int StockId { get; set; }
public int TransactionCount { get; set; }
public DateTime DateOfAggregation { get; set; }
public decimal o { get; set; } // open
public decimal l { get; set; } // lo
public decimal h { get; set; } // hi
public decimal c { get; set; } // close
public decimal v { get; set; } // volume
public decimal vwa { get; set; } // volume weighted average
}
15 changes: 15 additions & 0 deletions DataAccess/Repositories/IPriceRepository.cs
@@ -0,0 +1,15 @@
using Common.Models;
using DataAccess.Entities;
using System.Collections.Generic;

namespace DataAccess.Repositories
{
public interface IPriceRepository
{
Price GetById(int id);
IEnumerable<Price> GetAll();
Price Add(PriceDTO price);
Price Update(int id, PriceDTO price);
void Delete(int id);
}
}
80 changes: 80 additions & 0 deletions DataAccess/Repositories/PriceRepository.cs
@@ -0,0 +1,80 @@
using AutoMapper;
using DataAccess.Context;
using DataAccess.Entities;
using Common.Models;
using System.Collections.Generic;
using System.Linq;

namespace DataAccess.Repositories
{
public class PriceRepository : IPriceRepository
{
private readonly IMapper _mapper;
private readonly RseContext _context;

public PriceRepository(RseContext context, IMapper mapper)
{
_mapper = mapper;
_context = context;
}

public IEnumerable<Price> GetAll()
{
var prices = _context.Prices;
return prices;
}

public Price GetById(int id)
{
return _context.Prices.FirstOrDefault(p => p.Id == id);
}

public Price Add(PriceDTO priceDto)
{
Price price = _mapper.Map<Price>(priceDto);
Price addedPrice = _context.Prices.Add(price).Entity;
_context.SaveChanges();
return addedPrice;
}

public Price Update(int id, PriceDTO priceDto)
{
Price price = _context.Prices.Find(id);
if (price == null)
{
// Handle not found scenario
}

var priceProps = typeof(Price).GetProperties();
var dtoProps = typeof(PriceDTO).GetProperties();

foreach (var prop in dtoProps)
{
if (prop.Name == "Id")
continue;

var priceProp = priceProps.FirstOrDefault(p => p.Name == prop.Name);
if (priceProp != null && prop.GetValue(priceDto) != null)
{
priceProp.SetValue(price, prop.GetValue(priceDto));
}
}

_context.SaveChanges();

return price;
}

public void Delete(int id)
{
Price price = _context.Prices.Find(id);
if (price == null)
{
// Handle not found scenario
}

_context.Prices.Remove(price);
_context.SaveChanges();
}
}
}
2 changes: 2 additions & 0 deletions DataAccess/UnitOfWork/IUnitOfWork.cs
Expand Up @@ -6,5 +6,7 @@ namespace DataAccess.UnitOfWork;
public interface IUnitOfWork : IDisposable
{
IStockRepository StockRepository { get; }
IPriceRepository PriceRepository { get; }

// void Save() {};
}
14 changes: 14 additions & 0 deletions DataAccess/UnitOfWork/UnitOfWork.cs
Expand Up @@ -9,12 +9,14 @@ public class UnitOfWork : IUnitOfWork
private readonly RseContext _context;
private readonly IMapper _mapper;
private IStockRepository _stockRepository;
private IPriceRepository _priceRepository;

public UnitOfWork(RseContext context, IMapper mapper)
{
_mapper = mapper;
_context = context;
_stockRepository = new StockRepository(_context, _mapper);
_priceRepository = new PriceRepository(_context, _mapper);
}

public IStockRepository StockRepository
Expand All @@ -28,6 +30,18 @@ public IStockRepository StockRepository
return _stockRepository;
}
}

public IPriceRepository PriceRepository
{
get
{
if (_priceRepository == null)
{
_priceRepository = new PriceRepository(_context, _mapper);
}
return _priceRepository;
}
}

public void Save()
{
Expand Down

0 comments on commit e50eeb7

Please sign in to comment.