<commit_before>﻿using Peasy;
using Peasy.Core;
using Orders.com.BLL.Commands;
using Orders.com.BLL.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Orders.com.BLL.Domain;
using Orders.com.BLL.DataProxy;
using Orders.com.BLL.Extensions;

namespace Orders.com.BLL.Services
{
    public class OrderItemService : OrdersDotComServiceBase<OrderItem>, IOrderItemService
    {
        private IProductDataProxy _productDataProxy;
        private ITransactionContext _transactionContext;
        private IInventoryItemDataProxy _inventoryDataProxy;

        public OrderItemService(IOrderItemDataProxy dataProxy,
                                IProductDataProxy productDataProxy,
                                IInventoryItemDataProxy inventoryDataProxy,
                                ITransactionContext transactionContext) : base(dataProxy)
        {
            _productDataProxy = productDataProxy;
            _inventoryDataProxy = inventoryDataProxy;
            _transactionContext = transactionContext;
        }

        protected override void OnInsertCommandInitialization(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            entity.OrderStatus().SetPendingState();
        }

        protected override async Task OnInsertCommandInitializationAsync(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            OnInsertCommandInitialization(entity, context);
        }

        protected override IEnumerable<IRule> GetBusinessRulesForInsert(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            var currentProduct = _productDataProxy.GetByID(entity.ProductID);
            yield return new OrderItemPriceValidityRule(entity, currentProduct);
            yield return new OrderItemAmountValidityRule(entity, currentProduct);
        }

        protected override async Task<IEnumerable<IRule>> GetBusinessRulesForInsertAsync(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            var currentProduct = await _productDataProxy.GetByIDAsync(entity.ProductID);
            return new IRule[]
            {
                new OrderItemPriceValidityRule(entity, currentProduct),
                new OrderItemAmountValidityRule(entity, currentProduct)
            };
        }

        protected override IEnumerable<IRule> GetBusinessRulesForUpdate(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            var currentProduct = _productDataProxy.GetByID(entity.ProductID);
            yield return new ValidOrderItemStatusForUpdateRule(entity)
                                .IfValidThenValidate
                                (
                                    new OrderItemPriceValidityRule(entity, currentProduct),
                                    new OrderItemAmountValidityRule(entity, currentProduct)
                                );
        }

        protected override async Task<IEnumerable<IRule>> GetBusinessRulesForUpdateAsync(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            var currentProduct = await _productDataProxy.GetByIDAsync(entity.ProductID);
            return new ValidOrderItemStatusForUpdateRule(entity)
                                .IfValidThenValidate
                                (
                                    new OrderItemPriceValidityRule(entity, currentProduct),
                                    new OrderItemAmountValidityRule(entity, currentProduct)
                                )
                                .ToArray();
        }

        protected override IEnumerable<IRule> GetBusinessRulesForDelete(long id, ExecutionContext<OrderItem> context)<commit_msg>add logic to load orderItem and revert non-editable before returning business rules<commit_after>﻿using Peasy;
using Peasy.Core;
using Orders.com.BLL.Commands;
using Orders.com.BLL.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Orders.com.BLL.Domain;
using Orders.com.BLL.DataProxy;
using Orders.com.BLL.Extensions;
using Peasy.Extensions;

namespace Orders.com.BLL.Services
{
    public class OrderItemService : OrdersDotComServiceBase<OrderItem>, IOrderItemService
    {
        private IProductDataProxy _productDataProxy;
        private ITransactionContext _transactionContext;
        private IInventoryItemDataProxy _inventoryDataProxy;

        public OrderItemService(IOrderItemDataProxy dataProxy,
                                IProductDataProxy productDataProxy,
                                IInventoryItemDataProxy inventoryDataProxy,
                                ITransactionContext transactionContext) : base(dataProxy)
        {
            _productDataProxy = productDataProxy;
            _inventoryDataProxy = inventoryDataProxy;
            _transactionContext = transactionContext;
        }

        protected override void OnInsertCommandInitialization(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            entity.OrderStatus().SetPendingState();
        }

        protected override async Task OnInsertCommandInitializationAsync(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            OnInsertCommandInitialization(entity, context);
        }

        protected override IEnumerable<IRule> GetBusinessRulesForInsert(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            var currentProduct = _productDataProxy.GetByID(entity.ProductID);
            yield return new OrderItemPriceValidityRule(entity, currentProduct);
            yield return new OrderItemAmountValidityRule(entity, currentProduct);
        }

        protected override async Task<IEnumerable<IRule>> GetBusinessRulesForInsertAsync(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            var currentProduct = await _productDataProxy.GetByIDAsync(entity.ProductID);
            return new IRule[]
            {
                new OrderItemPriceValidityRule(entity, currentProduct),
                new OrderItemAmountValidityRule(entity, currentProduct)
            };
        }

        protected override IEnumerable<IRule> GetBusinessRulesForUpdate(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            var currentEntity = _dataProxy.GetByID(entity.ID);
            context.CurrentEntity = currentEntity;
            entity.RevertNonEditableValues(currentEntity);
            var currentProduct = _productDataProxy.GetByID(entity.ProductID);

            yield return new ValidOrderItemStatusForUpdateRule(entity)
                                .IfValidThenValidate
                                (
                                    new OrderItemPriceValidityRule(entity, currentProduct),
                                    new OrderItemAmountValidityRule(entity, currentProduct)
                                );
        }

        protected override async Task<IEnumerable<IRule>> GetBusinessRulesForUpdateAsync(OrderItem entity, ExecutionContext<OrderItem> context)
        {
            var currentEntity = await _dataProxy.GetByIDAsync(entity.ID);
            context.CurrentEntity = currentEntity;
            entity.RevertNonEditableValues(currentEntity);
            var currentProduct = await _productDataProxy.GetByIDAsync(entity.ProductID);

            return new ValidOrderItemStatusForUpdateRule(entity)
                                .IfValidThenValidate
                                (
                                    new OrderItemPriceValidityRule(entity, currentProduct),
                                    new OrderItemAmountValidityRule(entity, currentProduct)
                                )
                                .ToArray();
        }

        protected override IEnumerable<IRule> GetBusinessRulesForDelete(long id, ExecutionContext<OrderItem> context)
{'fraction_non_alphanumeric': 0.05657511707378813, 'fraction_numerical': 0.0, 'mean_word_length': 2.310431503979891, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3302323', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1767, 'n_words': 353}