<commit_before>﻿using System;
using System.Linq;
using AutoMapper;
using BoozeHoundCloud.Areas.Core.DataTransferObjects;
using BoozeHoundCloud.Areas.Core.Exceptions;
using BoozeHoundCloud.Areas.Core.Models;
using BoozeHoundCloud.DataAccess;
using BoozeHoundCloud.Models;

namespace BoozeHoundCloud.Areas.Core.Services
{
  internal class TransactionService : ITransactionService
  {
    //-------------------------------------------------------------------------

    private readonly IApplicationDbContext _context;
    private readonly IRepository<Transaction> _transactions;
    private readonly IAccountService _accounts;

    //-------------------------------------------------------------------------

    public static TransactionService Create(IApplicationDbContext context)
    {
      return new TransactionService(
        context,
        new GenericRepository<Transaction>(context),
        AccountService.Create(context));
    }

    //-------------------------------------------------------------------------

    public TransactionService(IApplicationDbContext context,
                              IRepository<Transaction> transactions,
                              IAccountService accounts)
    {
      if (context == null)
      {
        throw new ArgumentException("Context cannot be null.", nameof(context));
      }

      if (transactions == null)
      {
        throw new ArgumentException("Transaction repository cannot be null.", nameof(transactions));
      }

      if (accounts == null)
      {
        throw new ArgumentException("Account service cannot be null", nameof(accounts));
      }

      _context = context;
      _transactions = transactions;
      _accounts = accounts;
    }

    //-------------------------------------------------------------------------

    public int AddTransaction(TransactionDto newTransaction)
    {
      var transaction = Mapper.Map<TransactionDto, Transaction>(newTransaction);

      SetCreatedTimestamp(transaction);
      SetProcessedTimestampToNull(transaction);
      ResolveAccounts(newTransaction, transaction);
      UpdateAccountBalances(transaction);
      AddTransactionAndSave(transaction);

      return transaction.Id;
    }

    //-------------------------------------------------------------------------

    public void UpdateTransaction(Transaction modifiedTransaction)
    {
      Transaction orignalTransaction = _transactions.Get(modifiedTransaction.Id);

      if (orignalTransaction == null)
      {
        throw new ArgumentException($"Transaction not found with id {modifiedTransaction.Id}.");
      }

      if (modifiedTransaction.Value != orignalTransaction.Value)
      {
        throw new BusinessLogicException("Transaction value cannot change.");
      }

      if (modifiedTransaction.DebitAccountId != orignalTransaction.DebitAccountId)
      {
        throw new BusinessLogicException("Transaction debit-account id cannot change.");
      }

      if (modifiedTransaction.CreditAccountId != orignalTransaction.CreditAccountId)
      {
        throw new BusinessLogicException("Transaction credit-account id cannot change.");
      }

      if (modifiedTransaction.Date != orignalTransaction.Date)
      {
        throw new BusinessLogicException("Transaction date cannot change.");
      }

      if ((modifiedTransaction.CreatedTimestamp - orignalTransaction.CreatedTimestamp).Ticks != 0)<commit_msg>Another stab at the timestamp compare.<commit_after>﻿using System;
using System.Linq;
using AutoMapper;
using BoozeHoundCloud.Areas.Core.DataTransferObjects;
using BoozeHoundCloud.Areas.Core.Exceptions;
using BoozeHoundCloud.Areas.Core.Models;
using BoozeHoundCloud.DataAccess;
using BoozeHoundCloud.Models;

namespace BoozeHoundCloud.Areas.Core.Services
{
  internal class TransactionService : ITransactionService
  {
    //-------------------------------------------------------------------------

    private readonly IApplicationDbContext _context;
    private readonly IRepository<Transaction> _transactions;
    private readonly IAccountService _accounts;

    //-------------------------------------------------------------------------

    public static TransactionService Create(IApplicationDbContext context)
    {
      return new TransactionService(
        context,
        new GenericRepository<Transaction>(context),
        AccountService.Create(context));
    }

    //-------------------------------------------------------------------------

    public TransactionService(IApplicationDbContext context,
                              IRepository<Transaction> transactions,
                              IAccountService accounts)
    {
      if (context == null)
      {
        throw new ArgumentException("Context cannot be null.", nameof(context));
      }

      if (transactions == null)
      {
        throw new ArgumentException("Transaction repository cannot be null.", nameof(transactions));
      }

      if (accounts == null)
      {
        throw new ArgumentException("Account service cannot be null", nameof(accounts));
      }

      _context = context;
      _transactions = transactions;
      _accounts = accounts;
    }

    //-------------------------------------------------------------------------

    public int AddTransaction(TransactionDto newTransaction)
    {
      var transaction = Mapper.Map<TransactionDto, Transaction>(newTransaction);

      SetCreatedTimestamp(transaction);
      SetProcessedTimestampToNull(transaction);
      ResolveAccounts(newTransaction, transaction);
      UpdateAccountBalances(transaction);
      AddTransactionAndSave(transaction);

      return transaction.Id;
    }

    //-------------------------------------------------------------------------

    public void UpdateTransaction(Transaction modifiedTransaction)
    {
      Transaction orignalTransaction = _transactions.Get(modifiedTransaction.Id);

      if (orignalTransaction == null)
      {
        throw new ArgumentException($"Transaction not found with id {modifiedTransaction.Id}.");
      }

      if (modifiedTransaction.Value != orignalTransaction.Value)
      {
        throw new BusinessLogicException("Transaction value cannot change.");
      }

      if (modifiedTransaction.DebitAccountId != orignalTransaction.DebitAccountId)
      {
        throw new BusinessLogicException("Transaction debit-account id cannot change.");
      }

      if (modifiedTransaction.CreditAccountId != orignalTransaction.CreditAccountId)
      {
        throw new BusinessLogicException("Transaction credit-account id cannot change.");
      }

      if (modifiedTransaction.Date != orignalTransaction.Date)
      {
        throw new BusinessLogicException("Transaction date cannot change.");
      }

      if ((int)(modifiedTransaction.CreatedTimestamp - orignalTransaction.CreatedTimestamp).TotalSeconds != 0)
{'fraction_non_alphanumeric': 0.17748286422633805, 'fraction_numerical': 0.00029167274318214965, 'mean_word_length': 2.1073855913004076, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1084219', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1488, 'n_words': 352}