Aliendreamer

discountsales xmlprocessing excercise

Aug 1st, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. //mapping
  2.   CreateMap<Sale, ExportSaleDto>()
  3.                 .ForMember(cfg => cfg.Discount, opt => opt.MapFrom(x => Math.Round(x.Discount,2)))
  4.                 .ForMember(cfg => cfg.CustomerName, opt => opt.MapFrom(x => x.Customer.Name))
  5.                 .ForMember(cfg=>cfg.ExportFerariCars,opt=>opt.MapFrom(x=>x.Car))
  6.                 .ForMember(cfg => cfg.Price,
  7.                     opt => opt.MapFrom(x => Math.Round(x.Car.Parts.Sum(p => p.Part.Price),2)));
  8.  
  9. //dtos
  10.  
  11.     [XmlType("sale")]
  12.     public class ExportSaleDto
  13.     {
  14.         [XmlIgnore]
  15.         public decimal Pricewithdiscount;
  16.         [XmlElement("car")]
  17.         public ExportFerariCars ExportFerariCars { get; set; }
  18.  
  19.         [XmlElement("customer-name")]
  20.         public string CustomerName { get; set; }
  21.  
  22.         [XmlElement("discount")]
  23.         public double Discount { get; set; }
  24.  
  25.         [XmlElement("price")]
  26.         public decimal Price { get; set; }
  27.  
  28.         // i needed to make PriceWithDiscount this way or xmlserializer couldn't see it
  29.         // strange but true and it worked
  30.         [XmlElement("price-with-discount")]
  31.         public decimal PriceWithDiscount
  32.         {
  33.             get => Math.Round(this.Price -= this.Price * (decimal)this.Discount, 2);
  34.             set => Pricewithdiscount = value;
  35.         }
  36.     }
  37. ............
  38.    public class ExportFerariCars
  39.     {
  40.         [XmlAttribute("make")]
  41.         public string Make { get; set; }
  42.  
  43.         [XmlAttribute("model")]
  44.         public string Model { get; set; }
  45.  
  46.         [XmlAttribute("travelled-distance")]
  47.         public long Distance { get; set; }
  48.     }
  49. //method for the query:
  50.     public static void SalesWithAppliedDiscount(IMapper mapper, CarDealerContext context)
  51.         {
  52.             var sales = context.Sales.ProjectTo<ExportSaleDto>(mapper.ConfigurationProvider).ToArray();
  53.  
  54.             StringBuilder sb = new StringBuilder();
  55.             var serializer = new XmlSerializer(typeof(ExportSaleDto[]), new XmlRootAttribute("sales"));
  56.             var xmlNameSpaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
  57.             serializer.Serialize(new StringWriter(sb), sales, xmlNameSpaces);
  58.             File.WriteAllText("../../../Xml/sales-discounts.xml", sb.ToString());
  59.  
  60.         }
  61. //result:
  62. <sales>
  63.   <sale>
  64.     <car make="Ferrari" model="F430" travelled-distance="214647" />
  65.     <customer-name>Marcelle Griego</customer-name>
  66.     <discount>0.05</discount>
  67.     <price>7243.30</price>
  68.     <price-with-discount>6881.14</price-with-discount>
  69.   </sale>
  70.   <sale>
  71.     <car make="Ferrari" model="250 GTO" travelled-distance="213647" />
  72.     <customer-name>Hipolito Lamoreaux</customer-name>
  73.     <discount>0.1</discount>
  74.     <price>6073.98</price>
  75.     <price-with-discount>5466.58</price-with-discount>
  76.   </sale>
  77.   <sale>........
Advertisement
Add Comment
Please, Sign In to add comment