• EF Api 全套


    using MoNiLianXi_Api.EF;
    using MoNiLianXi_Api.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    namespace MoNiLianXi_Api.Controllers
    {
        public class DefaultController : ApiController
        {
            //ef显示
            [Route("api/Search")]
            [HttpGet]
            public IHttpActionResult Search()
            {
                List<RedPacketViewModel> list = new List<RedPacketViewModel>();
                using (var ef = new Model1())
                {
                    list = (from s in ef.RedPacket
                            select new RedPacketViewModel
                            {
                                ID = s.ID,
                                ActivityName = s.ActivityName,
                                Balance = s.Balance,
                                BalanceNum = s.BalanceNum,
                                Common = s.Common,
                                CreateTime = s.CreateTime,
                                Name = s.Name,
                                Num = s.Num,
                                Number = s.Number,
                                Remark = s.Remark,
                                State = s.State,
                                Type = s.Type
                            }).ToList();
                }
                return Ok(list);
            }
            [Route("api/SSearch")]
            [HttpGet]
            public IHttpActionResult SSearch()
            {
                List<RedPacketViewModel> list = new List<RedPacketViewModel>();
                using (var ef = new Model1())
                {
                    list = (from s in ef.RedState
                            select new RedPacketViewModel
                            {
                                SID = s.SID,
                                SName = s.SName
                            }).ToList();
                }
                return Ok(list);
            }
            [Route("api/TSearch")]
            [HttpGet]
            public IHttpActionResult TSearch()
            {
                List<RedPacketViewModel> list = new List<RedPacketViewModel>();
                using (var ef = new Model1())
                {
                    list = (from s in ef.RedType
                            select new RedPacketViewModel
                            {
                                TID = s.TID,
                                TName = s.TName
                            }).ToList();
                }
                return Ok(list);
            }
            //条件查询
            [Route("api/Searchs")]
            [HttpGet]
            public IHttpActionResult Searchs(string name)
            {
                List<RedPacketViewModel> list = new List<RedPacketViewModel>();
                using (var ef = new Model1())
                {
                    list = (from s in ef.RedPacket
                            where s.Name.Contains(name)
                            select new RedPacketViewModel
                            {
                                ID = s.ID,
                                ActivityName = s.ActivityName,
                                Balance = s.Balance,
                                BalanceNum = s.BalanceNum,
                                Common = s.Common,
                                CreateTime = s.CreateTime,
                                Name = s.Name,
                                Num = s.Num,
                                Number = s.Number,
                                Remark = s.Remark,
                                State = s.State,
                                Type = s.Type
                            }).ToList();
                }
                return Ok(list);
            }
            //添加
            [Route("api/Insert")]
            [HttpPost]
            public int Insert(RedPacketViewModel m)
            {
                int flag = 0;
                using (var ef = new Model1())
                {
                    RedPacket red = new RedPacket
                    {
                        ActivityName = m.ActivityName,
                        Balance = m.Balance,
                        BalanceNum = m.BalanceNum,
                        Common = m.Common,
                        CreateTime = DateTime.Now,
                        Name = m.Name,
                        Num = m.Num,
                        Number = m.Number,
                        Remark = m.Remark,
                        State = m.State,
                        Type = m.Type
                    };
                    ef.Entry(red).State = System.Data.Entity.EntityState.Added;
                    flag = ef.SaveChanges();
                }
                return flag;
            }
            //删除
            [Route("api/Del")]
            [HttpPost]
            public int Del(RedPacketViewModel m)
            {
                int falg = 0;
                using (var ef = new Model1())
                {
                    var list = (from s in ef.RedPacket where s.ID == m.ID select s).FirstOrDefault();
                    ef.Entry(list).State = System.Data.Entity.EntityState.Deleted;
                    falg = ef.SaveChanges();
                }
                return falg;
            }
            //修改
            [Route("api/Upt")]
            [HttpPost]
            public int Upt(RedPacketViewModel m)
            {
                int falg = 0;
                using (var ef = new Model1())
                {
                    var list = (from s in ef.RedPacket where s.ID == m.ID select s).FirstOrDefault();
                    list.Name = m.Name; list.Num = m.Num; list.Number = m.Number;
                    list.Remark = m.Remark; list.ActivityName = m.ActivityName; list.Balance = m.Balance;
                    list.BalanceNum = m.BalanceNum; list.Common = m.Common;
                    list.State = m.State; list.Type = m.Type;
                    ef.Entry(list).State = System.Data.Entity.EntityState.Modified;
                    falg = ef.SaveChanges();
                }
                return falg;
            }
            //反填
            [Route("api/Fill")]
            [HttpGet]
            public IHttpActionResult Fill(int ID)
            {
                RedPacketViewModel list = new RedPacketViewModel();
                using (var ef = new Model1())
                {
                    list = (from s in ef.RedPacket
                            where s.ID == ID
                            select new RedPacketViewModel
                            {
                                ID = s.ID,
                                ActivityName = s.ActivityName,
                                Balance = s.Balance,
                                BalanceNum = s.BalanceNum,
                                Common = s.Common,
                                CreateTime = s.CreateTime,
                                Name = s.Name,
                                Num = s.Num,
                                Number = s.Number,
                                Remark = s.Remark,
                                State = s.State,
                                Type = s.Type
                            }).FirstOrDefault();
                }
                return Ok(list);
            }
      /// <summary>
            /// 显示清单表
            /// </summary>
            /// <param name="pageNumber">分页的页数</param>
            /// <param name="pageSize">每页显示数量</param>
            /// <param name="name">根据红包发送人进行查询</param>
            /// <param name="Stime">开始时间</param>
            /// <param name="Etime">结束时间</param>
            /// <returns></returns>
            [Route("api/show")]
            public IHttpActionResult Get(int pageNumber = 1, int pageSize = 10, string name = "", string Stime = "", string Etime = "")
            {
                List<TransactionViewModel> list = new List<TransactionViewModel>();
                using (var ef = new MyContext())
                {
                    list = (from u in ef.userInfos
                            join t in ef.transactions on u.Id equals t.UId
                            select new TransactionViewModel
                            {
                                balance = (from a in ef.amountDetails where a.UId == u.Id select a.Amount).FirstOrDefault(),
                                dateTime = t.dateTime,
                                ExpenditureAmount = t.ExpenditureAmount,
                                Id = t.Id,
                                UId = t.UId,
                                Income = t.Income,
                                TransferName = t.TransferName,
                                UserName = u.UserName,
                                TransferQQ = t.TransferQQ,
                                Status = t.status
                            }).ToList();
                    //查询条件
                    if (!string.IsNullOrEmpty(name))
                    {
                        list = list.Where(s => s.UserName.Contains(name)).ToList();
                    }
                    if (!(string.IsNullOrEmpty(Stime) && string.IsNullOrEmpty(Etime)))
                    {
                        list = list.Where(s => s.dateTime >= DateTime.Parse(Stime) && s.dateTime <= DateTime.Parse(Etime)).ToList();
                    }
                }
                return Ok(new { total = list.Count, rows = list.Skip((pageNumber - 1) * pageSize).Take(pageSize) });
            }

        }
    }
  • 相关阅读:
    Windows 获取unix timestamp
    SQL Server 2008 R2:快速清除日志文件的方法
    mysql lost connection to server during query
    jquery 隐藏 显示 动画效果
    session
    javaScript日期
    路径惹的祸
    Declaration terminated incorrectly 讨厌 这样就不可以了
    jsp 调用其他jsp页面 跳转
    SQL 2008 启用和禁用xp_cmdshell
  • 原文地址:https://www.cnblogs.com/GuoLianSheng/p/13267974.html
一二三 - 开发者的网上家园