/// <summary>
        /// 处理Post请求信息
        /// </summary>
        /// <param name="mapping"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        protected async Task <IActionResult> _PostRequest(Func <T, Task <T> > mapping, Func <T, Task <IActionResult> > handle = null)
        {
            var accid     = AuthMan.GetAccountId(this);
            var metadata  = new T();
            var canCreate = await _Repository.CanCreateAsync(accid);

            if (!canCreate)
            {
                return(Forbid());
            }
            var data = await mapping(metadata);

            await _Repository.SatisfyCreateAsync(accid, data, ModelState);

            if (!ModelState.IsValid)
            {
                return(new ValidationFailedResult(ModelState));
            }
            await _Repository.CreateAsync(accid, data);

            var dto = await _Repository.GetByIdAsync(metadata.Id);

            //如果handle不为空,由handle掌控ActionResult
            if (handle != null)
            {
                return(await handle(data));
            }
            return(Ok(dto));
        }
        /// <summary>
        /// 根据id获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        protected async Task <IActionResult> _GetByIdRequest(string id, Func <DTO, Task <IActionResult> > handle = null)
        {
            var accid = AuthMan.GetAccountId(this);
            var exist = await _Repository.ExistAsync(id);

            if (!exist)
            {
                return(NotFound());
            }
            var canRead = await _Repository.CanReadAsync(accid, id);

            if (!canRead)
            {
                return(Forbid());
            }

            var dto = await _Repository.GetByIdAsync(id);

            //如果handle不为空,由handle掌控ActionResult
            if (handle != null)
            {
                return(await handle(dto));
            }
            return(Ok(dto));
        }
        /// <summary>
        /// 处理Delete请求
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        protected async Task <IActionResult> _DeleteRequest(string id)
        {
            var accid = AuthMan.GetAccountId(this);
            var exist = await _Repository.ExistAsync(id);

            if (!exist)
            {
                return(NotFound());
            }

            var data = await _Repository._GetByIdAsync(id);

            await _Repository.SatisfyDeleteAsync(accid, data, ModelState);

            if (!ModelState.IsValid)
            {
                return(new ValidationFailedResult(ModelState));
            }

            var canDelete = await _Repository.CanDeleteAsync(accid, id);

            if (!canDelete)
            {
                return(Forbid());
            }
            await _Repository.DeleteAsync(accid, id);

            return(Ok());
        }
        public async Task <IActionResult> UpdateAdditionalRole([FromBody] UserAdditionalRoleEditModel model)
        {
            var     accid = AuthMan.GetAccountId(this);
            Account acc   = await _Repository._DbContext.Accounts.Where(x => x.Id == model.UserId).FirstOrDefaultAsync();

            //清除之前的角色信息
            var originRoles = _Repository._DbContext.AccountRoles.Where(x => x.Account == acc);

            foreach (var originItem in originRoles)
            {
                _Repository._DbContext.Remove(originItem);
            }
            await _Repository._DbContext.SaveChangesAsync();

            var newRoles  = new List <AccountRole>();
            var roleIdArr = model.AdditionalRoleIds.Split(",", StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();

            foreach (var roleId in roleIdArr)
            {
                var newRole = new AccountRole();
                newRole.Id         = GuidGen.NewGUID();
                newRole.Account    = acc;
                newRole.UserRoleId = roleId;
                newRoles.Add(newRole);
            }
            acc.AdditionRoles = newRoles;
            _Repository._DbContext.Update(acc);
            await _Repository._DbContext.SaveChangesAsync();

            return(Ok());
        }
        /*********************Collection*********************/

        #region _PostCollectionRequest 处理添加收藏数据请求
        /// <summary>
        /// 处理添加收藏数据请求
        /// </summary>
        /// <param name="mapping"></param>
        /// <returns></returns>
        protected async Task <IActionResult> _PostCollectionRequest(Func <Collection, Task <Collection> > mapping)
        {
            var accid   = AuthMan.GetAccountId(this);
            var account = await _Repository._DbContext.Accounts.FindAsync(accid);

            var metadata = new Collection();
            //var canCreate = await _Repository.CanCreateAsync(accid);
            //if (!canCreate)
            //    return Forbid();
            var data = await mapping(metadata);

            data.Id             = GuidGen.NewGUID();
            data.CreatedTime    = DateTime.UtcNow;
            data.ModifiedTime   = DateTime.UtcNow;
            data.Creator        = accid;
            data.Modifier       = accid;
            data.OrganizationId = account.OrganizationId;
            _Repository._DbContext.Collections.Add(data);
            await _Repository._DbContext.SaveChangesAsync();

            //await _Repository.SatisfyCreateAsync(accid, data, ModelState);
            //if (!ModelState.IsValid)
            //    return new ValidationFailedResult(ModelState);
            //await _Repository.CreateAsync(accid, data);
            //var dto = await _Repository.GetByIdAsync(metadata.Id);
            ////如果handle不为空,由handle掌控ActionResult
            //if (handle != null)
            //    return await handle(data);
            return(Ok());
        }
예제 #6
0
        public async Task <IActionResult> ChangeContent(string id, [FromBody] OrderContent content)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            string accid = AuthMan.GetAccountId(this);
            var    ok    = await repo.CanUpdateAsync(accid, id);

            if (ok == false)
            {
                return(Forbid());
            }

            var res = await repo.GetAsync(accid, id);

            if (res == null)
            {
                return(NotFound());
            }

            res.Content    = Newtonsoft.Json.JsonConvert.SerializeObject(content);
            res.ModifyTime = DateTime.UtcNow;
            await repo.SaveChangesAsync();

            return(Ok());
        }
예제 #7
0
        public async Task <IActionResult> UpdateData(string id, [FromBody] string data)
        {
            if (data == null)
            {
                data = "";
            }
            string accid = AuthMan.GetAccountId(this);
            var    ok    = await repo.CanUpdateAsync(accid, id);

            if (ok == false)
            {
                return(Forbid());
            }

            var obj = await repo.GetAsync(accid, id);

            if (obj == null)
            {
                return(Forbid());
            }
            obj.Data = data;
            await repo.SaveChangesAsync();

            return(Ok());
        }
예제 #8
0
        public async Task <IActionResult> TransToLayout([FromBody] MapTransLayoutModel model)
        {
            var map = await _Repository._GetByIdAsync(model.MapId);

            var accid  = AuthMan.GetAccountId(this);
            var layout = new Layout();

            layout.Id             = GuidGen.NewGUID();
            layout.Name           = map.Name;
            layout.Description    = string.Format("auto generate at {0}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            layout.OrganizationId = map.OrganizationId;
            layout.Icon           = map.Icon;
            layout.Creator        = accid;
            layout.Modifier       = accid;
            layout.CreatedTime    = DateTime.Now;
            layout.ModifiedTime   = DateTime.Now;
            layout.ActiveFlag     = AppConst.I_DataState_Active;
            var layoutData = new MapTransLayoutDataModel();

            layoutData.Map   = layout.Name;
            layoutData.MapId = map.Id;
            layout.Data      = JsonConvert.SerializeObject(layoutData);
            _Repository._DbContext.Layouts.Add(layout);
            await _Repository._DbContext.SaveChangesAsync();

            return(Ok());
        }
        /// <summary>
        /// 根据查询参数获取分页信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="qMapping"></param>
        /// <param name="advanceQuery"></param>
        /// <param name="literal"></param>
        /// <returns></returns>
        protected async Task <IActionResult> _GetPagingRequest(PagingRequestModel model, Action <List <string> > qMapping = null, Func <IQueryable <T>, Task <IQueryable <T> > > advanceQuery = null, Func <T, IList <T>, Task <T> > literal = null)
        {
            var accid = AuthMan.GetAccountId(this);
            var qs    = new List <string>();

            qMapping?.Invoke(qs);
            if (qs.Count > 0)
            {
                var builder = new StringBuilder();
                foreach (var item in qs)
                {
                    builder.AppendFormat(";{0}", item);
                }
                model.Q = builder.ToString();
            }
            var result = await _Repository.SimplePagedQueryAsync(model, accid, advanceQuery);

            if (literal != null)
            {
                if (result.Data != null && result.Data.Count > 0)
                {
                    for (int idx = result.Data.Count - 1; idx >= 0; idx--)
                    {
                        result.Data[idx] = await literal(result.Data[idx], result.Data);
                    }
                }
            }
            return(Ok(RepositoryBase <T, DTO> .PageQueryDTOTransfer(result)));
        }
예제 #10
0
        public IActionResult Post([FromBody] FileAsset value)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            value = repo.Create(AuthMan.GetAccountId(this), value);
            return(CreatedAtAction("Get", value));
        }
예제 #11
0
        public IActionResult Get(string id)
        {
            var res = repo.Get(AuthMan.GetAccountId(this), id);

            if (res == null)
            {
                return(NotFound());
            }
            return(Ok(res));//return Forbid();
        }
예제 #12
0
        public async Task <IActionResult> Delete(string id)
        {
            bool bOk = await repo.DeleteAsync(AuthMan.GetAccountId(this), id);

            if (bOk)
            {
                return(Ok());
            }
            return(NotFound());//return Forbid();
        }
예제 #13
0
        public async Task <IActionResult> Get(string id)
        {
            var res = await repo.GetAsync(AuthMan.GetAccountId(this), id);

            if (res == null)
            {
                return(NotFound());
            }
            return(Ok(res));//return Forbid();
        }
예제 #14
0
        public IActionResult Delete(string id)
        {
            bool bOk = repo.Delete(AuthMan.GetAccountId(this), id);

            if (bOk)
            {
                return(Ok());
            }
            return(NotFound());//return Forbid();
        }
        public async Task <IActionResult> ResetPassword([FromBody] ResetPasswordModel model)
        {
            var     accid = AuthMan.GetAccountId(this);
            Account acc   = await _Repository._DbContext.Accounts.FindAsync(model.UserId);

            acc.Password = model.Password;
            _Repository._DbContext.Update(acc);
            await _Repository._DbContext.SaveChangesAsync();

            return(Ok());
        }
        public async Task <IActionResult> GetProfile()
        {
            var accid = AuthMan.GetAccountId(this);
            var exist = await _Repository._DbContext.Accounts.AnyAsync(x => x.Id == accid);

            if (!exist)
            {
                return(NotFound());
            }
            return(await Get(accid));
        }
예제 #17
0
        public async Task <IActionResult> Post([FromBody] Solution value)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            value = await repo.CreateAsync(AuthMan.GetAccountId(this), value);

            return(CreatedAtAction("Get", value));
        }
예제 #18
0
        public async Task <IActionResult> Get(string id)
        {
            var res = await repo.GetAsync(AuthMan.GetAccountId(this), id);

            if (res == null)
            {
                return(NotFound());
            }
            repo.Context.Entry(res).Collection(d => d.Specifications).Load();
            return(Ok(res));//return Forbid();
        }
예제 #19
0
        /// <summary>
        /// 获取当前用户的组织Id
        /// </summary>
        protected async Task <string> _GetCurrentUserOrganId()
        {
            var accid   = AuthMan.GetAccountId(this);
            var account = await _Repository._DbContext.Accounts.FirstOrDefaultAsync(x => x.Id == accid);

            if (account != null)
            {
                return(account.OrganizationId);
            }
            return(string.Empty);
        }
예제 #20
0
        public async Task <IActionResult> Get(string id)
        {
            var res = await repo.GetAsync(AuthMan.GetAccountId(this), id);

            if (res == null)
            {
                return(NotFound());
            }
            //加载Order的依赖数据,OrderStates
            repo.Context.Entry(res).Collection(d => d.OrderStates).Load();
            return(Ok(res));
        }
예제 #21
0
        /// <summary>
        /// 获取当前用户的组织
        /// </summary>
        /// <returns></returns>
        protected async Task <Organization> _GetCurrentUserOrgan()
        {
            var accid   = AuthMan.GetAccountId(this);
            var account = await _Repository._DbContext.Accounts.FirstOrDefaultAsync(x => x.Id == accid);

            if (account != null)
            {
                var organ = await _Repository._DbContext.Organizations.FirstOrDefaultAsync(x => x.Id == account.OrganizationId);

                return(organ);
            }
            return(null);
        }
예제 #22
0
        public IActionResult NewPassword([FromBody] NewPasswordModel value)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }
            bool bOk = accountMan.ChangePassword(AuthMan.GetAccountId(this), value);

            if (bOk)
            {
                return(Ok());
            }
            return(BadRequest());
        }
예제 #23
0
        public IActionResult EditProfile([FromBody] AccountProfileModel value)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }
            bool bOk = accountMan.UpdateProfile(AuthMan.GetAccountId(this), value);

            if (bOk)
            {
                return(Ok());
            }
            return(BadRequest());
        }
예제 #24
0
        public async Task <IActionResult> Upload()
        {
            Action <string> saveFile = (path) =>
            {
                using (StreamWriter sw = new StreamWriter(path))
                {
                    HttpContext.Request.Body.CopyTo(sw.BaseStream);
                }
            };
            var accid   = AuthMan.GetAccountId(this);
            var account = await _Repository._DbContext.Accounts.FindAsync(accid);

            return(await SaveUpload(saveFile, account));
        }
예제 #25
0
        /// <summary>
        /// 处理导出数据请求
        /// </summary>
        /// <typeparam name="CSV"></typeparam>
        /// <param name="model"></param>
        /// <param name="transMapping"></param>
        /// <param name="qMapping"></param>
        /// <param name="advanceQuery"></param>
        /// <returns></returns>
        protected async Task <IActionResult> _ExportDataRequest <CSV>(PagingRequestModel model, Func <DTO, Task <CSV> > transMapping, Action <List <string> > qMapping = null, Func <IQueryable <T>, Task <IQueryable <T> > > advanceQuery = null)
            where CSV : ClassMap, new()
        {
            model.Page     = 0;
            model.PageSize = int.MaxValue;
            var accid = AuthMan.GetAccountId(this);
            var qs    = new List <string>();

            qMapping?.Invoke(qs);
            if (qs.Count > 0)
            {
                var builder = new StringBuilder();
                foreach (var item in qs)
                {
                    builder.AppendFormat(";{0}", item);
                }
                model.Q = builder.ToString();
            }

            var list = new List <CSV>();
            var res  = await _Repository.SimplePagedQueryAsync(model, accid, advanceQuery);

            var resource = RepositoryBase <T, DTO> .PageQueryDTOTransfer(res);

            if (resource.Data != null && resource.Data.Count > 0)
            {
                foreach (var item in resource.Data)
                {
                    var csData = await transMapping(item);

                    list.Add(csData);
                }
            }
            var ms = new MemoryStream();

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream, Encoding.UTF8))
                    using (var csv = new CsvWriter(writer))
                    {
                        csv.WriteHeader <CSV>();
                        csv.NextRecord();
                        csv.WriteRecords(list);
                        writer.Flush();
                        stream.Position = 0;
                        stream.CopyTo(ms);
                        ms.Position = 0;
                    }
            return(File(ms, "application/octet-stream"));
        }
        public async Task <IActionResult> GetUserNav(string role)
        {
            var accid   = AuthMan.GetAccountId(this);
            var account = await _Repository._DbContext.Accounts.FirstOrDefaultAsync(x => x.Id == accid);

            var userNav = await _Repository._DbContext.UserNavs.Include(x => x.UserNavDetails).Where(x => x.Role == account.Type).FirstOrDefaultAsync();

            if (userNav == null)
            {
                return(Ok(new List <UserNavDetail>()));
            }
            var dto = await _Repository.GetByIdAsync(userNav.Id);

            return(Ok(dto.UserNavDetails));
        }
예제 #27
0
        public async Task <IActionResult> Put([FromBody] Solution value)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            var res = await repo.UpdateAsync(AuthMan.GetAccountId(this), value);

            if (res == null)
            {
                return(NotFound());
            }
            return(Ok(value));
        }
        public async Task <NavigationModel> GetNavigationData()
        {
            var     accid = AuthMan.GetAccountId(this);
            Account acc   = await _Repository._DbContext.Accounts.FindAsync(accid);

            if (acc != null)
            {
                NavigationModel mm;
                if (SiteConfig.Instance.GetItem("navi_" + acc.Type, out mm))
                {
                    return(mm);
                }
            }
            return(null);
        }
예제 #29
0
        public IActionResult Put([FromBody] FileAsset value)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            var res = repo.Update(AuthMan.GetAccountId(this), value);

            if (res == null)
            {
                return(NotFound());
            }
            return(Ok(value));
        }
예제 #30
0
        public async Task <IActionResult> Get([FromQuery] PagingRequestModel model, string categoryId = "", string categoryName = "", bool classify = true)
        {
            var advanceQuery = new Func <IQueryable <Product>, Task <IQueryable <Product> > >(async(query) =>
            {
                if (classify)
                {
                    #region 根据分类Id查询
                    if (!string.IsNullOrWhiteSpace(categoryId))
                    {
                        var curCategoryTree = await _Repository._DbContext.AssetCategoryTrees.FirstOrDefaultAsync(x => x.ObjId == categoryId);
                        //如果是根节点,把所有取出,不做分类过滤
                        //if (curCategoryTree != null && curCategoryTree.LValue > 1)
                        //{
                        var categoryQ = from it in _Repository._DbContext.AssetCategoryTrees
                                        where it.NodeType == curCategoryTree.NodeType && it.OrganizationId == curCategoryTree.OrganizationId &&
                                        it.LValue >= curCategoryTree.LValue && it.RValue <= curCategoryTree.RValue
                                        select it;
                        query = from it in query
                                join cat in categoryQ on it.CategoryId equals cat.ObjId
                                select it;
                        //}
                    }
                    //else
                    //{
                    //    query = query.Where(x => !string.IsNullOrWhiteSpace(x.CategoryId));
                    //}
                    #endregion

                    #region 根据分类名称查询
                    if (!string.IsNullOrWhiteSpace(categoryName))
                    {
                        var accid       = AuthMan.GetAccountId(this);
                        var account     = await _Repository._DbContext.Accounts.FindAsync(accid);
                        var categoryIds = _Repository._DbContext.AssetCategories.Where(x => x.Type == AppConst.S_Category_Product && x.OrganizationId == account.OrganizationId && x.Name.Contains(categoryName)).Select(x => x.Id);
                        query           = query.Where(x => categoryIds.Contains(x.CategoryId));
                    }
                    #endregion
                }
                else
                {
                    query = query.Where(x => string.IsNullOrWhiteSpace(x.CategoryId));
                }
                query = query.Where(x => x.ActiveFlag == AppConst.I_DataState_Active);
                return(await Task.FromResult(query));
            });

            return(await _GetPagingRequest(model, null, advanceQuery));
        }