123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using Luticate2.Utils.DataAccess;
- using Luticate2.Utils.Dbo.Filter;
- using Luticate2.Utils.Dbo.Result;
- using Microsoft.EntityFrameworkCore;
- using TestUtils.DataAccess.Models;
- using TestUtils.Dbo.PkGuid;
-
- namespace TestUtils.DataAccess
- {
- public class LuUtilsPkGuidDataAccess : LuEfCrudDataAccess<pk_guid, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, LuUtilsDbContext, string>
- {
- public LuUtilsPkGuidDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
- {
- }
-
- protected override DbSet<pk_guid> GetTable(LuUtilsDbContext db)
- {
- return db.pk_guid;
- }
-
- protected override pk_guid GetModelFromTCreate(PkGuidAddDbo obj)
- {
- return GetModelFromTUpdate(obj, new pk_guid());
- }
-
- protected override void EditModelFromTUpdate(PkGuidAddDbo obj, pk_guid model)
- {
- model.some_int = obj.SomeInt;
- model.some_text = obj.SomeText;
- }
-
- public static PkGuidDbo GetDboFromModelStatic(pk_guid model)
- {
- return new PkGuidDbo
- {
- CreatedAt = model.created_at,
- UpdatedAt = model.updated_at,
- Id = model.id.ToString(),
- SomeInt = model.some_int,
- SomeText = model.some_text
- };
- }
-
- protected override PkGuidDbo GetDboFromModel(pk_guid model)
- {
- return GetDboFromModelStatic(model);
- }
-
- protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
- {
- return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());
- }
-
- public LuResult<bool> SomeTextExists(string someText)
- {
- return Execute((db, table) => LuResult<bool>.Ok(table.Any(guid => guid.some_text == someText)));
- }
-
- public override LuResult<T> Add<T>(IEnumerable<PkGuidAddDbo> objs, Func<IEnumerable<PkGuidDbo>, T> returnFunc)
- {
- IList<pk_guid> models = new List<pk_guid>();
- var addRes = Execute((db, table) =>
- {
- var transact = BeginTransaction(db);
- foreach (var dbo in objs)
- {
- var quoteRequest = GetModelFromTCreate(dbo);
- models.Add(quoteRequest);
- table.Add(quoteRequest);
- }
- db.SaveChanges();
- foreach (var model in models)
- {
- db.Entry(model).State = EntityState.Detached;
- }
- foreach (var pkGuid in models)
- {
- if (pkGuid.some_int == 2424)
- {
- throw new Exception("Test unexpected db error");
- }
- if (pkGuid.some_int == 4242)
- {
- RollbackTransaction(transact);
- return LuResult<T>.Error(LuStatus.DbError, "Some expected error", "");
- }
- }
- CommitTransaction(transact);
- return LuResult<T>.Ok(default(T));
- });
- if (!addRes)
- {
- return addRes;
- }
- return GetMultiple(models, returnFunc);
- }
- }
- }
|