2025-02-27 16:30:42 +04:00

70 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PuferFishContracts.BusinessLogicsContracts;
using PuferFishContracts.DataModels;
using PuferFishContracts.Enums;
using PuferFishContracts.Exceptions;
using PuferFishContracts.Extensions;
using PuferFishContracts.StoragesContracts;
namespace PuferFishBusinessLogic.Implementations;
internal class PointsBusinessLogicContract(IPointsStorageContract pointsStorageContract, ISaleStorageContract saleStorageContract, IBuyerStorageContract buyerStorageContract, ILogger logger) : IPointsBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IPointsStorageContract _pointsStorageContract = pointsStorageContract;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
private readonly IBuyerStorageContract _buyerStorageContract = buyerStorageContract;
public List<PointsDataModel> GetAllPointsByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}",
fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _pointsStorageContract.GetList(fromDate, toDate) ?? throw new
NullListException();
}
public List<PointsDataModel> GetAllPointsByPeriodByBuyer(DateTime fromDate, DateTime toDate, string buyerId)
{
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (buyerId.IsEmpty())
{
throw new ArgumentNullException(nameof(buyerId));
}
if (!buyerId.IsGuid())
{
throw new ValidationException("The value in the field buyerId is not a unique identifier.");
}
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, { buyerId}", fromDate, toDate, buyerId);
return _pointsStorageContract.GetList(fromDate, toDate, buyerId) ??
throw new NullListException();
}
public void CalculatePointsByMounth(DateTime date)
{
_logger.LogInformation("CalculatePointsByMonth: {date}", date);
var startDate = new DateTime(date.Year, date.Month, 1);
var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
var buyers = _buyerStorageContract.GetList() ?? throw new NullListException();
foreach (var buyer in buyers)
{
var points = buyer.Points;
_logger.LogDebug("The buyer {buyerId} was awarded {points} points", buyer.Id, points);
_pointsStorageContract.AddElement(new PointsDataModel(buyer.Id, finishDate, points));
}
}
}