Advertisement
Somo4k

Untitled

Aug 19th, 2022
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 KB | None | 0 0
  1. using System.Text;
  2. using Castle.Core.Internal;
  3. using Newtonsoft.Json;
  4. using Trucks.Data.Models;
  5. using Trucks.Data.Models.Enums;
  6. using Trucks.DataProcessor.ImportDto;
  7.  
  8. namespace Trucks.DataProcessor
  9. {
  10.     using System;
  11.     using System.Collections.Generic;
  12.     using System.ComponentModel.DataAnnotations;
  13.     using System.Linq;
  14.  
  15.     using Data;
  16.  
  17.     public class Deserializer
  18.     {
  19.         private const string ErrorMessage = "Invalid data!";
  20.  
  21.         private const string SuccessfullyImportedDespatcher
  22.             = "Successfully imported despatcher - {0} with {1} trucks.";
  23.  
  24.         private const string SuccessfullyImportedClient
  25.             = "Successfully imported client - {0} with {1} trucks.";
  26.  
  27.         public static string ImportDespatcher(TrucksContext context, string xmlString)
  28.         {
  29.             var sb = new StringBuilder();
  30.  
  31.             var despatcherDto = XmlConverter.Deserializer<ImportDespatchersXML>(xmlString, "Despatchers");
  32.  
  33.             var valiDespatchers = new List<Despatcher>();
  34.  
  35.  
  36.             foreach (var dDto in despatcherDto)
  37.             {
  38.  
  39.                 if (!IsValid(dDto))
  40.                 {
  41.                     sb.AppendLine(ErrorMessage);
  42.                     continue;
  43.                 }
  44.  
  45.                 if (dDto.Position.IsNullOrEmpty())
  46.                 {
  47.                     sb.AppendLine(ErrorMessage);
  48.                     continue;
  49.                 }
  50.  
  51.                 var trucks = new List<Truck>();
  52.  
  53.  
  54.                 foreach (var tDto in dDto.Trucks)
  55.                 {
  56.  
  57.                     if (!IsValid(tDto))
  58.                     {
  59.                         sb.AppendLine(ErrorMessage);
  60.                         continue;
  61.                     }
  62.  
  63.  
  64.                     if (tDto.VinNumber.IsNullOrEmpty())
  65.                     {
  66.                         sb.AppendLine(ErrorMessage);
  67.                         continue;
  68.                     }
  69.  
  70.  
  71.                     trucks.Add(new Truck()
  72.                     {
  73.                         RegistrationNumber = tDto.RegistrationNumber,
  74.                         VinNumber = tDto.VinNumber,
  75.                         TankCapacity = tDto.TankCapacity,
  76.                         CargoCapacity = tDto.CargoCapacity,
  77.                         CategoryType = (CategoryType)tDto.CategoryType,
  78.                         MakeType = (MakeType)tDto.MakeType
  79.                     });
  80.  
  81.  
  82.                 }
  83.  
  84.                 Despatcher despatcher = new Despatcher()
  85.                 {
  86.                     Name = dDto.Name,
  87.                     Position = dDto.Position,
  88.                     Trucks = trucks
  89.                 };
  90.  
  91.                 valiDespatchers.Add(despatcher);
  92.  
  93.                 sb.AppendLine(string.Format(SuccessfullyImportedDespatcher, despatcher.Name, trucks.Count()));
  94.             }
  95.  
  96.             context.Despatchers.AddRange(valiDespatchers);
  97.             context.SaveChanges();
  98.  
  99.             return sb.ToString().TrimEnd();
  100.  
  101.  
  102.  
  103.         }
  104.         public static string ImportClient(TrucksContext context, string jsonString)
  105.         {
  106.             var sb = new StringBuilder();
  107.  
  108.             ImportCleintsDTOJson[] clientsDto = JsonConvert.DeserializeObject<ImportCleintsDTOJson[]>(jsonString);
  109.  
  110.             List<Client> validClients = new List<Client>();
  111.            
  112.             foreach (var cDTO in clientsDto)
  113.             {
  114.  
  115.                 if (!IsValid(cDTO))
  116.                 {
  117.                     sb.AppendLine(ErrorMessage);
  118.                     continue;
  119.                 }
  120.  
  121.  
  122.                 Client client = new Client()
  123.                 {
  124.                     Name = cDTO.Name,
  125.                     Nationality = cDTO.Nationality,
  126.                     Type = cDTO.Type
  127.                 };
  128.  
  129.  
  130.                 if (client.Type == "usual")
  131.                 {
  132.                     sb.AppendLine(ErrorMessage);
  133.                     continue;
  134.                 }
  135.  
  136.                 foreach (var tDTO in cDTO.Trucks.Distinct())
  137.                 {
  138.  
  139.                     Truck truck = context.Trucks.FirstOrDefault(f => f.Id == tDTO);
  140.  
  141.                     if (truck == null)
  142.                     {
  143.                         sb.AppendLine(ErrorMessage);
  144.                         continue;
  145.                     }
  146.  
  147.                     if (!IsValid(tDTO))
  148.                     {
  149.                         sb.AppendLine(ErrorMessage);
  150.                         continue;
  151.                     }
  152.  
  153.                     ClientTruck clientTruck = new ClientTruck()
  154.                     {
  155.                         Client = client,
  156.                         Truck = truck
  157.                     };
  158.  
  159.                     client.ClientsTrucks.Add(clientTruck);
  160.  
  161.                 }
  162.  
  163.                 validClients.Add(client);
  164.                 sb.AppendLine(string.Format(SuccessfullyImportedClient, client.Name, client.ClientsTrucks.Count()));
  165.  
  166.             }
  167.  
  168.             context.Clients.AddRange(validClients);
  169.             context.SaveChanges();
  170.             return sb.ToString().TrimEnd();
  171.         }
  172.  
  173.         private static bool IsValid(object dto)
  174.         {
  175.             var validationContext = new ValidationContext(dto);
  176.             var validationResult = new List<ValidationResult>();
  177.  
  178.             return Validator.TryValidateObject(dto, validationContext, validationResult, true);
  179.         }
  180.     }
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement