using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AutoMapper; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface; using Microsoft.Extensions.Options; using Datory; namespace GxPress.Repository.Implement { public class SlideRepository : ISlideRepository { private readonly Repository _repository; private readonly IMapper _mapper; public SlideRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task ExistsAsync(int id) { return await _repository.ExistsAsync(id); } public async Task GetAsync(int id) { var slide = await _repository.GetAsync(id); slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl); return slide; } public async Task> GetListAsync() { var slides= await _repository.GetAllAsync(Q.OrderBy(nameof(Slide.Sort))); var slideList = slides.ToList(); foreach (var slide in slideList) { slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl); } return slideList; } /// /// 添加 /// /// /// public async Task AddAsync(Slide request) { var sort = await _repository.MaxAsync(nameof(Slide.Sort)) ?? 0; request.Sort = sort + 1; request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl); request.Id = await _repository.InsertAsync(request); return request; } public async Task EditAsync(Slide request) { request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl); await _repository.UpdateAsync(request); return request; } public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } } }