Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuUtilsPkGuidDataAccess.cs 3.3KB

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