public async Task <FuncResult> AddOrUpdateController([FromBody] ControllerRouteModel model)
 {
     return(await sysRouteBLL.AddOrUpdateController(model, CurrentUser.Get().Id));
 }
Ejemplo n.º 2
0
        public async Task <FuncResult> AddOrUpdateController(ControllerRouteModel model, int currentUserId)
        {
            if (string.IsNullOrWhiteSpace(model.ControllerPath))
            {
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "Controller路径不能为空"
                });
            }
            if (!Regex.Match(model.ControllerPath, @"^[a-zA-Z0-9]+$").Success)
            {
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "ControllerPath只能输入字母、数字"
                });
            }
            if (string.IsNullOrWhiteSpace(model.ControllerAlias))
            {
                model.ControllerAlias = model.ControllerPath;
            }
            if (_context.SysControllerRoutes.Where(e => e.Id != model.Id).Select(e => e.ControllerPath.ToLower()).Contains(model.ControllerPath))
            {
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "路径重复"
                });
            }
            if (_context.SysControllerRoutes.Where(e => e.Id != model.Id).Select(e => e.ControllerAlias.ToLower()).Contains(model.ControllerAlias))
            {
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "别名重复"
                });
            }



            SysControllerRoute entity = new SysControllerRoute();

            if (!string.IsNullOrWhiteSpace(model.Id))
            {
                entity = await _context.SysControllerRoutes.FindAsync(model.Id);

                if (entity == null)
                {
                    return(new FuncResult()
                    {
                        IsSuccess = false, Message = "id错误"
                    });
                }
                entity.ControllerAlias = model.ControllerAlias;
                entity.ControllerPath  = model.ControllerPath;
                entity.AreaId          = model.AreaId;
                entity.IsApi           = model.IsApi;


                entity.Last_Updated_By  = currentUserId;
                entity.Last_Update_Date = DateTime.Now;
                _context.SysControllerRoutes.Update(entity);
                await _context.SaveChangesAsync();

                return(new FuncResult()
                {
                    IsSuccess = true, Content = entity, Message = "编辑成功"
                });
            }

            entity.Id = Guid.NewGuid().ToString("N");
            entity.ControllerAlias = model.ControllerAlias;
            entity.ControllerPath  = model.ControllerPath;
            entity.AreaId          = model.AreaId;
            entity.IsApi           = model.IsApi;

            entity.Last_Updated_By  = currentUserId;
            entity.Last_Update_Date = DateTime.Now;
            entity.Created_By       = currentUserId;
            entity.Creation_Date    = DateTime.Now;

            await _context.SysControllerRoutes.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(new FuncResult()
            {
                IsSuccess = true, Content = entity, Message = "添加成功"
            });
        }
Ejemplo n.º 3
0
        public async Task <FuncResult> AddOrUpdateController(ControllerRouteModel model, string currentUserId)
        {
            if (string.IsNullOrWhiteSpace(model.ControllerPath))
            {
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "Controller路径不能为空"
                });
            }
            if (!Regex.Match(model.ControllerPath, @"^[a-zA-Z0-9]+$").Success)
            {
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "ControllerPath只能输入字母、数字"
                });
            }
            if (string.IsNullOrWhiteSpace(model.ControllerAlias))
            {
                model.ControllerAlias = model.ControllerPath;
            }
            if (_context.SysControllerRoute.Where(e => e.SysControllerRouteId != model.Id).Select(e => e.ControllerPath.ToLower()).Contains(model.ControllerPath))
            {
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "路径重复"
                });
            }
            if (_context.SysControllerRoute.Where(e => e.SysControllerRouteId != model.Id).Select(e => e.ControllerAlias.ToLower()).Contains(model.ControllerAlias))
            {
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "别名重复"
                });
            }



            SysControllerRoute entity = new SysControllerRoute();

            if (!string.IsNullOrWhiteSpace(model.Id))
            {
                entity = await _context.SysControllerRoute.FindAsync(model.Id);

                if (entity == null)
                {
                    return(new FuncResult()
                    {
                        IsSuccess = false, Message = "id错误"
                    });
                }
                entity.ControllerAlias = model.ControllerAlias;
                entity.ControllerPath  = model.ControllerPath;
                entity.AreaId          = model.AreaId;
                entity.IsApi           = model.IsApi?1:0;
                entity.SortValue       = model.SortValue;

                entity.LastUpdatedBy  = currentUserId;
                entity.LastUpdateDate = DateTime.Now;
                _context.SysControllerRoute.Update(entity);
                await _context.SaveChangesAsync();

                return(new FuncResult()
                {
                    IsSuccess = true, Content = entity, Message = "编辑成功"
                });
            }

            entity.SysControllerRouteId = Guid.NewGuid().ToString("N");
            entity.ControllerAlias      = model.ControllerAlias;
            entity.ControllerPath       = model.ControllerPath;
            entity.AreaId         = model.AreaId;
            entity.IsApi          = model.IsApi?1:0;
            entity.SortValue      = model.SortValue;
            entity.LastUpdatedBy  = currentUserId;
            entity.LastUpdateDate = DateTime.Now;
            entity.CreatedBy      = currentUserId;
            entity.CreationDate   = DateTime.Now;
            try
            {
                await _context.SysControllerRoute.AddAsync(entity);

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                LogService.WriteError(ex);
                return(new FuncResult()
                {
                    IsSuccess = false, Message = "发生了预料之外的错误"
                });
            }
            return(new FuncResult()
            {
                IsSuccess = true, Content = entity, Message = "添加成功"
            });
        }