RocketService.cs 2.1 KB
Newer Older
lwplvx's avatar
lwplvx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aurora.Core.Domain.Entities;
using Aurora.Core.EntityFramework;
using Aurora.Core.IService;
using Aurora.Core.IService.Dto;
using Aurora.Infrastructure.Response;
using AutoMapper;
using Microsoft.EntityFrameworkCore;

namespace Aurora.Core.Service
{
    public class RocketService : IRocketService
    {
        private readonly ApplicationDbContext _dbContext;
        private readonly ApplicationReadonlyDbContext _readonlyDbContext;
        private readonly IMapper _mapper;

        public RocketService(ApplicationDbContext dbContext,
            ApplicationReadonlyDbContext readonlyDbContext,
            IMapper mapper)
        {
            _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));

            _dbContext = dbContext;
            _readonlyDbContext = readonlyDbContext;

        }
        public async Task<ResponseModel<List<RocketDto>>> GetList()
        {
            var list = await _readonlyDbContext.Categories.ToListAsync();
            var dtoList = _mapper.Map<List<RocketDto>>(list);

            var res = new ResponseModel<List<RocketDto>>(dtoList);

            return res;
        }

        public async Task<ResponseModel<RocketDto>> GetById(int id)
        {
            var entity = await _readonlyDbContext.Categories.FirstOrDefaultAsync(m => m.ID == id);
            var dto = _mapper.Map<RocketDto>(entity);

            var res = new ResponseModel<RocketDto>(dto);

            return res;
        }

        public async Task<ResponseModel<RocketDto>> Rocket(RocketDto model)
        {
lwplvx's avatar
lwplvx 已提交
53 54
            #region 保存到数据库

lwplvx's avatar
lwplvx 已提交
55 56
            var entity = _mapper.Map<RocketWord>(model);

lwplvx's avatar
lwplvx 已提交
57
            _dbContext.RocketWords.Add(entity);
lwplvx's avatar
lwplvx 已提交
58 59 60

            var num = await _dbContext.SaveChangesAsync();

lwplvx's avatar
lwplvx 已提交
61 62 63 64 65 66 67 68 69
            #endregion

            #region 发送到移动端

            // 按照用户发送
            // 广播发送

            #endregion

lwplvx's avatar
lwplvx 已提交
70 71 72 73 74 75 76 77
            var dto = _mapper.Map<RocketDto>(entity);

            var res = new ResponseModel<RocketDto>(dto);

            return res;
        }
    }
}