ServiceValidationTestingMethod.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using AutoServiceSultik;
  2. using AutoServiceSultik.Entites;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ServiceUnitTest
  10. {
  11. public static class ServiceValidationTestingMethod
  12. {
  13. private static Service[] Services = new Service[2]
  14. {
  15. new Service() { Title = "Замена жидкости в кондиционере", Cost = 3040, DurationInSeconds = 28800},
  16. new Service() { Title = "Ремонт и замена коллектора", Cost = 2770, DurationInSeconds = 9000, Discount = 0.15}
  17. };
  18. public static string CheckErrors(string name, string costStr, string discountStr)
  19. {
  20. var errorBuider = new StringBuilder();
  21. if (string.IsNullOrEmpty(name))
  22. errorBuider.AppendLine("Название услуги обязательно для заполнения;");
  23. var serviceFromDB = Services.FirstOrDefault(p => p.Title.ToLower() == name.ToLower());
  24. if (serviceFromDB != null)
  25. errorBuider.AppendLine("Такая услуга уже существует;");
  26. decimal cost = 0;
  27. if (decimal.TryParse(costStr, out cost) == false || cost <= 0)
  28. errorBuider.AppendLine("Стоимость услуги должно быть положительное число;");
  29. if (!string.IsNullOrEmpty(discountStr))
  30. {
  31. int discoutn = 0;
  32. if (int.TryParse(discountStr, out discoutn) == false || discoutn < 0 || discoutn > 100)
  33. errorBuider.AppendLine("Размер скидки - целое число в диапазоне от 0 до 100%");
  34. }
  35. return errorBuider.ToString();
  36. }
  37. }
  38. }