using AutoMapper; using BarCode.Web.Domain.Entitys; using BarCode.Web.Domain.Infrastructure; using BarCode.Web.Domain.Values; using BarCode.Web.Repositories.Configuration; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BarCode.Web.Repositories { /// /// /// public class CenerateDataRepositories : ICenerateDataRepositories { private readonly IMapper _mapper; private readonly IServiceProvider _serviceProvider; private readonly RepositoryDbContext _context; public CenerateDataRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider) { _context = context; _mapper = mapper; _serviceProvider = serviceProvider; } /// /// 添加 /// /// /// /// public async Task Add(CenerateData entity, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); await _context.CenerateData.AddAsync(entity); await _context.SaveChangesAsync(); if (_transaction != null) _transaction.Commit(); return entity; } /// /// 修改 /// /// /// /// public async Task Edit(CenerateData entity, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); var res = await _context.CenerateData .FirstOrDefaultAsync(f => f.Id == entity.Id); if (res == null) return null; _mapper.Map(entity, res); await _context.SaveChangesAsync(); if (_transaction != null) _transaction.Commit(); return res; } public async Task Get(CenerateDataType type) { var entity = await _context.CenerateData .FirstOrDefaultAsync(f => f.Type == type); if (entity == null) { entity = await this.Add(new CenerateData(type)); return entity; } return entity; } } }