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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Utils.DataAccess;
  6. using Luticate2.Utils.Dbo.Filter;
  7. using Luticate2.Utils.Dbo.Result;
  8. using Microsoft.EntityFrameworkCore;
  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 void EditModelFromTUpdate(PkGuidAddDbo obj, pk_guid model)
  27. {
  28. model.some_int = obj.SomeInt;
  29. model.some_text = obj.SomeText;
  30. }
  31. public static PkGuidDbo GetDboFromModelStatic(pk_guid model)
  32. {
  33. return new PkGuidDbo
  34. {
  35. CreatedAt = model.created_at,
  36. UpdatedAt = model.updated_at,
  37. Id = model.id.ToString(),
  38. SomeInt = model.some_int,
  39. SomeText = model.some_text
  40. };
  41. }
  42. protected override PkGuidDbo GetDboFromModel(pk_guid model)
  43. {
  44. return GetDboFromModelStatic(model);
  45. }
  46. protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
  47. {
  48. return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());
  49. }
  50. public LuResult<bool> SomeTextExists(string someText)
  51. {
  52. return Execute((db, table) => LuResult<bool>.Ok(table.Any(guid => guid.some_text == someText)));
  53. }
  54. public override LuResult<T> Add<T>(IEnumerable<PkGuidAddDbo> objs, Func<IEnumerable<PkGuidDbo>, T> returnFunc)
  55. {
  56. IList<pk_guid> models = new List<pk_guid>();
  57. var addRes = Execute((db, table) =>
  58. {
  59. var transact = BeginTransaction(db);
  60. foreach (var dbo in objs)
  61. {
  62. var quoteRequest = GetModelFromTCreate(dbo);
  63. models.Add(quoteRequest);
  64. table.Add(quoteRequest);
  65. }
  66. db.SaveChanges();
  67. foreach (var model in models)
  68. {
  69. db.Entry(model).State = EntityState.Detached;
  70. }
  71. foreach (var pkGuid in models)
  72. {
  73. if (pkGuid.some_int == 2424)
  74. {
  75. throw new Exception("Test unexpected db error");
  76. }
  77. if (pkGuid.some_int == 4242)
  78. {
  79. RollbackTransaction(transact);
  80. return LuResult<T>.Error(LuStatus.DbError, "Some expected error", "");
  81. }
  82. }
  83. CommitTransaction(transact);
  84. return LuResult<T>.Ok(default(T));
  85. });
  86. if (!addRes)
  87. {
  88. return addRes;
  89. }
  90. return GetMultiple(models, returnFunc);
  91. }
  92. }
  93. }