You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuUtilsPkGuidDataAccess.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using Luticate2.Utils.DataAccess;
  5. using Luticate2.Utils.Dbo.Filter;
  6. using Luticate2.Utils.Dbo.Result;
  7. using Microsoft.EntityFrameworkCore;
  8. using Npgsql;
  9. using TestUtils.DataAccess.Models;
  10. using TestUtils.Dbo.PkGuid;
  11. namespace TestUtils.DataAccess
  12. {
  13. public class LuUtilsPkGuidDataAccess : LuEfCrudDataAccess<pk_guid, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, LuUtilsDbContext, string>
  14. {
  15. public LuUtilsPkGuidDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
  16. {
  17. }
  18. protected override DbSet<pk_guid> GetTable(LuUtilsDbContext db)
  19. {
  20. return db.pk_guid;
  21. }
  22. protected override pk_guid GetModelFromTCreate(PkGuidAddDbo obj)
  23. {
  24. return GetModelFromTUpdate(obj, new pk_guid());
  25. }
  26. protected override LuResult<T> HandleError<T>(Exception e)
  27. {
  28. if (e is DbUpdateException)
  29. {
  30. var pge = e.InnerException as PostgresException;
  31. if (pge != null)
  32. {
  33. if (pge.ConstraintName == "pk_guid_some_text_key")
  34. {
  35. return LuResult<T>.Error(LuStatus.InputError, e, "someText already exists");
  36. }
  37. if (pge.ConstraintName == "pkguid_some_text_check_insert")
  38. {
  39. return LuResult<T>.Error(LuStatus.InputError, e, "someText can not end with '_edited'");
  40. }
  41. if (pge.ConstraintName == "pkguid_some_text_check_update")
  42. {
  43. return LuResult<T>.Error(LuStatus.InputError, e, "someText must end with '_edited'");
  44. }
  45. }
  46. }
  47. return null;
  48. }
  49. protected override void EditModelFromTUpdate(PkGuidAddDbo obj, pk_guid model)
  50. {
  51. model.some_int = obj.SomeInt;
  52. model.some_text = obj.SomeText;
  53. }
  54. protected override PkGuidDbo GetDboFromModel(pk_guid model)
  55. {
  56. return model.ToDbo();
  57. }
  58. protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
  59. {
  60. if (filter == null)
  61. {
  62. return x => true;
  63. }
  64. var someText = filter.GetFilterString("someText", null);
  65. var someInt = filter.GetFilterInt("someInt", null);
  66. return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString())
  67. && (someText == null || LuUtilsDbContext.lu_texts_match(someText, model.some_text))
  68. && (someInt == null || model.some_int == someInt);
  69. }
  70. protected override LuResult<bool> _Add(pk_guid model, PkGuidAddDbo dbo, LuUtilsDbContext db, IQueryable<pk_guid> table)
  71. {
  72. if (dbo.SomeInt == 2424)
  73. {
  74. throw new Exception("Test unexpected db error");
  75. }
  76. if (dbo.SomeInt == 4242)
  77. {
  78. return LuResult<bool>.Error(LuStatus.DbError, "Some expected error", "");
  79. }
  80. return LuResult<bool>.Ok(true);
  81. }
  82. protected override LuResult<bool> _EditSingleById(pk_guid model, PkGuidAddDbo update, LuUtilsDbContext db, IQueryable<pk_guid> table)
  83. {
  84. return LuResult<bool>.Ok(true);
  85. }
  86. }
  87. }