server/aspnetapp/Controllers/WebApi/GoodsController.cs

(开头部分) 6KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

#nullable disable
using aspnetapp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;

namespace aspnetapp.Controllers.WebApi
{
    [Route("api/[controller]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        private readonly CounterContext _context;

        private IMemoryCache _memoryCache;

        public GoodsController(CounterContext context, IMemoryCache cache)
        {
            _context = context;
            this._memoryCache = cache ?? throw new ArgumentNullException(nameof(cache));
        }

        // GET: api/Goods
        [HttpGet]
        public async Task<ActionResult<IEnumerable<GoodsEntity>>> GetGoods()
        {
            return await _context.Goods.OrderBy(s => s.SortNo).AsNoTracking().ToListAsync();
        }

        [HttpGet("{CategoryId}")]
        public async Task<ActionResult<List<object>>> GetGoodsByCategoryId(int CategoryId)
        {
            // 当前菜品分类类别
            CategoryEntity thiscategory = await _context.Categories.AsNoTracking().FirstOrDefaultAsync(s => s.Id == CategoryId);

            //int oldCategoryId = CategoryId;
            //if (CategoryId > 3)
            //{
            //    switch (CategoryId)
            //    {
            //        case 4: CategoryId = 1; break;
            //        case 5: CategoryId = 2; break;
            //        case 6: CategoryId = 3; break;
            //    }
            //}

            //List<GoodsEntity> thisCategoryAllGoods = await _context.Goods.Where(s => s.CategoryId == CategoryId).AsNoTracking().ToListAsync().ConfigureAwait(false);

            // 所有菜品
            List<GoodsEntity> thisCategoryAllGoods = _memoryCache.GetOrCreate("AllGoods", entry =>
            {
                entry.SetAbsoluteExpiration(TimeSpan.FromHours(24));
                entry.SetSlidingExpiration(TimeSpan.FromHours(12));
                return _context.Goods.OrderBy(s => s.SortNo).AsNoTracking().ToList();
            });

            if (!thisCategoryAllGoods.Any())
            {
                thisCategoryAllGoods = _context.Goods.OrderBy(s => s.SortNo).AsNoTracking().ToList();
            }
后面还有 106 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。