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 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());
  60. }
  61. protected override LuResult<pk_guid> _Add(PkGuidAddDbo dbo, LuUtilsDbContext db, DbSet<pk_guid> table)
  62. {
  63. if (dbo.SomeInt == 2424)
  64. {
  65. throw new Exception("Test unexpected db error");
  66. }
  67. if (dbo.SomeInt == 4242)
  68. {
  69. return LuResult<pk_guid>.Error(LuStatus.DbError, "Some expected error", "");
  70. }
  71. return LuResult<pk_guid>.Ok(GetModelFromTCreate(dbo));
  72. }
  73. }
  74. }