選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LuUtilsPkGuidDataAccess.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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<bool> _Add(pk_guid model, 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<bool>.Error(LuStatus.DbError, "Some expected error", "");
  70. }
  71. return LuResult<bool>.Ok(true);
  72. }
  73. protected override LuResult<bool> _EditSingleById(pk_guid model, PkGuidAddDbo update, LuUtilsDbContext db, DbSet<pk_guid> table)
  74. {
  75. return LuResult<bool>.Ok(true);
  76. }
  77. }
  78. }