public async Task Handle(PlagiarismDoneAnalyzeNotification notification, CancellationToken cancellationToken)
        {
            var plagiarizedFunctionRepository = serviceProvider.CreateScope().ServiceProvider
                                                .GetRequiredService <ISuspectFunctionRepository>();
            var functionId = new FunctionId(notification.SuspectedFunctionId);

            var plagiarizedFunctions = notification.ComparedFunctions.Select(func =>
                                                                             new PlagiarizedFunctionEntity(new FunctionId(func.Id), func.Rate, func.Hash, notification.SuspectHash, _timeService.Now())).ToList();
            var suspectFunction = await plagiarizedFunctionRepository.FindByIdAsync(functionId) ??
                                  SuspectFunctionAggregate.CreateNew(functionId, notification.SuspectHash, new List <PlagiarizedFunctionEntity>());

            suspectFunction.SetPlagiarizedFunctions(plagiarizedFunctions);
            await plagiarizedFunctionRepository.SetAsync(suspectFunction);
        }
        private SuspectFunctionAggregate ToEntity(Function model)
        {
            var plagiarizedFunctions = GetFirstPlagiarizedFunctions(model)
                                       .Select(func =>
            {
                return(new PlagiarizedFunctionEntity(
                           new FunctionId(func.PlagiarizedFunctionId),
                           func.Rate,
                           func.PlagiarizedFunctionHash,
                           func.CheatingFunctionHash,
                           func.DetectionDate
                           ));
            })
                                       .ToList();

            return(SuspectFunctionAggregate.Restore(new FunctionId(model.Id), plagiarizedFunctions));
        }
        private async Task <Function> ToModel(SuspectFunctionAggregate aggregate)
        {
            var suspectedFunction = await FindAsync(aggregate.Id.Value) ?? new Function();

            var currentPlagiarizedFunctions = GetCurrentPlagiarizedFunctions(suspectedFunction);
            var newPlagiarizedFunctions     = GetNewPlagiarizedFunctions(aggregate, currentPlagiarizedFunctions);

            newPlagiarizedFunctions.ForEach(pF => suspectedFunction.PlagiarizedFunctions.Add(pF));
            currentPlagiarizedFunctions.ForEach(pF =>
            {
                var func                   = aggregate.PlagiarizedFunctions.First(f => f.Id.Value == pF.PlagiarizedFunctionId);
                pF.IsValid                 = func.IsValid;
                pF.Rate                    = func.Rate;
                pF.DetectionDate           = func.DetectionDate;
                pF.CheatingFunctionHash    = func.SuspectHash;
                pF.PlagiarizedFunctionHash = func.Hash;
                pF.PlagiarizedFunctionHash = func.SuspectHash;
            });
            return(suspectedFunction);
        }
 private static List <PlagiarismFunction> GetNewPlagiarizedFunctions(SuspectFunctionAggregate aggregate,
                                                                     IList <PlagiarismFunction> currentPlagiarizedFunctions)
 {
     return(aggregate.PlagiarizedFunctions
            .Where(pF =>
     {
         var currentFunc = currentPlagiarizedFunctions.FirstOrDefault(f =>
                                                                      f.PlagiarizedFunction.Id == pF.Id.Value
                                                                      );
         return currentFunc?.CheatingFunctionHash != pF.SuspectHash &&
         currentFunc?.PlagiarizedFunctionHash != pF.Hash;
     })
            .Select(pF => new PlagiarismFunction
     {
         CheatingFunctionId = aggregate.Id.Value,
         PlagiarizedFunctionId = pF.Id.Value,
         Rate = pF.Rate,
         CheatingFunctionHash = pF.SuspectHash,
         PlagiarizedFunctionHash = pF.Hash,
         DetectionDate = pF.DetectionDate,
         IsValid = pF.IsValid
     })
            .ToList());
 }
Esempio n. 5
0
 private SuspectFunctionAggregate GetEmptySuspectFunctionAggregate() =>
 SuspectFunctionAggregate.Restore(TestsHelper.GetNewFunctionId(), new List <PlagiarizedFunctionEntity>());