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; using GxPress.Request.Slide; using GxPress.Common.Page; 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(SlideSearchRequest request) { var result = new PagedList(); var query = Q.NewQuery(); if (!string.IsNullOrEmpty(request.KeyWord)) query.WhereLike(nameof(Slide.Title), $"%{request.KeyWord}%"); if (request.TypeId > 0) query.Where(nameof(Slide.TypeId), request.TypeId); result.Total = await _repository.CountAsync(query); var slides = await _repository.GetAllAsync(query.OrderBy(nameof(Slide.Sort)).ForPage(request.Page, request.PerPage)); foreach (var slide in slides) slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl); result.Items = slides; return result; } public async Task> GetListAsync(int typeId) { var query = Q.NewQuery(); if (typeId > 0) query.Where(nameof(Slide.TypeId), typeId); query.Where(nameof(Entity.Slide.IsDisable), false); var slides = await _repository.GetAllAsync(query.OrderBy(nameof(Slide.Sort))); return slides; } /// /// 添加 /// /// /// 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); } } }