|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Blog.Core.IServices; |
| 6 | +using Blog.Core.Model; |
| 7 | +using Blog.Core.Model.Models; |
| 8 | +using Microsoft.AspNetCore.Authorization; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.AspNetCore.Mvc; |
| 11 | + |
| 12 | +namespace Blog.Core.Controllers |
| 13 | +{ |
| 14 | + [Route("api/[controller]")] |
| 15 | + [ApiController] |
| 16 | + //[Authorize("Permission")] |
| 17 | + public class RoleController : ControllerBase |
| 18 | + { |
| 19 | + IRoleServices _roleServices; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// 构造函数 |
| 23 | + /// </summary> |
| 24 | + /// <param name="roleServices"></param> |
| 25 | + public RoleController(IRoleServices roleServices ) |
| 26 | + { |
| 27 | + _roleServices = roleServices; |
| 28 | + } |
| 29 | + |
| 30 | + // GET: api/User |
| 31 | + [HttpGet] |
| 32 | + public async Task<MessageModel<PageModel<Role>>> Get(int page = 1, string key = "") |
| 33 | + { |
| 34 | + var data = new MessageModel<PageModel<Role>>(); |
| 35 | + int intTotalCount = 100; |
| 36 | + int TotalCount = 0; |
| 37 | + int PageCount = 1; |
| 38 | + List<Role> Roles = new List<Role>(); |
| 39 | + |
| 40 | + Roles = await _roleServices.Query(a => a.IsDeleted != true ); |
| 41 | + |
| 42 | + if (!string.IsNullOrEmpty(key)) |
| 43 | + { |
| 44 | + Roles = Roles.Where(t => (t.Name != null && t.Name.Contains(key))).ToList(); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + //筛选后的数据总数 |
| 49 | + TotalCount = Roles.Count; |
| 50 | + //筛选后的总页数 |
| 51 | + PageCount = (Math.Ceiling(TotalCount.ObjToDecimal() / intTotalCount.ObjToDecimal())).ObjToInt(); |
| 52 | + |
| 53 | + Roles = Roles.OrderByDescending(d => d.Id).Skip((page - 1) * intTotalCount).Take(intTotalCount).ToList(); |
| 54 | + |
| 55 | + return new MessageModel<PageModel<Role>>() |
| 56 | + { |
| 57 | + msg = "获取成功", |
| 58 | + success = TotalCount >= 0, |
| 59 | + response = new PageModel<Role>() |
| 60 | + { |
| 61 | + page = page, |
| 62 | + pageCount = PageCount, |
| 63 | + dataCount = TotalCount, |
| 64 | + data = Roles, |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + // GET: api/User/5 |
| 71 | + [HttpGet("{id}")] |
| 72 | + public string Get(string id) |
| 73 | + { |
| 74 | + return "value"; |
| 75 | + } |
| 76 | + |
| 77 | + // POST: api/User |
| 78 | + [HttpPost] |
| 79 | + public async Task<MessageModel<string>> Post([FromBody] Role Role) |
| 80 | + { |
| 81 | + var data = new MessageModel<string>(); |
| 82 | + |
| 83 | + var id = (await _roleServices.Add(Role)); |
| 84 | + data.success = id > 0; |
| 85 | + if (data.success) |
| 86 | + { |
| 87 | + data.response = id.ObjToString(); |
| 88 | + data.msg = "添加成功"; |
| 89 | + } |
| 90 | + |
| 91 | + return data; |
| 92 | + } |
| 93 | + |
| 94 | + // PUT: api/User/5 |
| 95 | + [HttpPut] |
| 96 | + public async Task<MessageModel<string>> Put([FromBody] Role Role) |
| 97 | + { |
| 98 | + var data = new MessageModel<string>(); |
| 99 | + if (Role != null && Role.Id > 0) |
| 100 | + { |
| 101 | + data.success = await _roleServices.Update(Role); |
| 102 | + if (data.success) |
| 103 | + { |
| 104 | + data.msg = "更新成功"; |
| 105 | + data.response = Role?.Id.ObjToString(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return data; |
| 110 | + } |
| 111 | + |
| 112 | + // DELETE: api/ApiWithActions/5 |
| 113 | + [HttpDelete] |
| 114 | + public async Task<MessageModel<string>> Delete(int id) |
| 115 | + { |
| 116 | + var data = new MessageModel<string>(); |
| 117 | + if (id > 0) |
| 118 | + { |
| 119 | + var userDetail = await _roleServices.QueryByID(id); |
| 120 | + userDetail.IsDeleted = true; |
| 121 | + data.success = await _roleServices.Update(userDetail); |
| 122 | + if (data.success) |
| 123 | + { |
| 124 | + data.msg = "删除成功"; |
| 125 | + data.response = userDetail?.Id.ObjToString(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return data; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments