Advertisement
Somo4k

Untitled

Aug 19th, 2022
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 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.                 if (cDTO.Type == "usual")
  122.                 {
  123.                     sb.AppendLine(ErrorMessage);
  124.                     continue;
  125.                 }
  126.  
  127.                 Client client = new Client()
  128.                 {
  129.                     Name = cDTO.Name,
  130.                     Nationality = cDTO.Nationality,
  131.                     Type = cDTO.Type
  132.                 };
  133.  
  134.  
  135.                 foreach (var tDTO in cDTO.Trucks.Distinct())
  136.                 {
  137.  
  138.                     Truck truck = context.Trucks.FirstOrDefault(f => f.Id == tDTO);
  139.  
  140.                     if (truck == null)
  141.                     {
  142.                         sb.AppendLine(ErrorMessage);
  143.                         continue;
  144.                     }
  145.  
  146.                     if (!IsValid(tDTO))
  147.                     {
  148.                         sb.AppendLine(ErrorMessage);
  149.                         continue;
  150.                     }
  151.  
  152.                     ClientTruck clientTruck = new ClientTruck()
  153.                     {
  154.                         Client = client,
  155.                         Truck = truck
  156.                     };
  157.  
  158.                     client.ClientsTrucks.Add(clientTruck);
  159.  
  160.                 }
  161.  
  162.                 validClients.Add(client);
  163.                 sb.AppendLine(string.Format(SuccessfullyImportedClient, client.Name, client.ClientsTrucks.Count()));
  164.  
  165.             }
  166.  
  167.             context.Clients.AddRange(validClients);
  168.             context.SaveChanges();
  169.             return sb.ToString().TrimEnd();
  170.         }
  171.  
  172.         private static bool IsValid(object dto)
  173.         {
  174.             var validationContext = new ValidationContext(dto);
  175.             var validationResult = new List<ValidationResult>();
  176.  
  177.             return Validator.TryValidateObject(dto, validationContext, validationResult, true);
  178.         }
  179.     }
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement