1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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<Slide> _repository;
- private readonly IMapper _mapper;
- public SlideRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<Slide>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<bool> ExistsAsync(int id)
- {
- return await _repository.ExistsAsync(id);
- }
- public async Task<Slide> GetAsync(int id)
- {
- var slide = await _repository.GetAsync(id);
- slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl);
- return slide;
- }
- public async Task<IEnumerable<Slide>> 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;
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<Slide> 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<Slide> EditAsync(Slide request)
- {
- request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl);
- await _repository.UpdateAsync(request);
- return request;
- }
- public async Task<bool> DeleteAsync(int id)
- {
- return await _repository.DeleteAsync(id);
- }
- }
- }
|