private async Task <ActionResult <List <CategoryVM> > > GetDataAsync(long categoryID, enCategoryType categoryType, string searchText) { try { var query = this.GetDataQuery(); if (categoryID != 0) { query = query.Where(x => x.CategoryID == categoryID); } if (categoryType != enCategoryType.None) { query = query.Where(x => x.Type == (short)categoryType); } if (!string.IsNullOrEmpty(searchText)) { searchText = System.Web.HttpUtility.UrlDecode(searchText); query = query.Where(x => x.CategoryID != 0 && x.HierarchyText.Contains(searchText)); } var categoryList = await query.ToListAsync(); var parentIDs = categoryList.Where(x => x.ParentID.HasValue).Select(x => x.ParentID.Value).Distinct().ToArray(); var parentList = await this.GetDataQuery().Where(x => parentIDs.Contains(x.CategoryID)).ToListAsync(); var result = categoryList .OrderBy(x => x.HierarchyText) .Select(x => new { Category = x, ParentRow = parentList.Where(p => p.CategoryID == x.ParentID).FirstOrDefault() }) .Select(x => CategoryVM.Convert(x.Category, x.ParentRow)) .ToList(); return(this.OkResponse(result)); } catch (Exception ex) { return(this.ExceptionResponse(ex)); } }
internal async Task <ActionResult <CategoryVM> > CreateAsync(CategoryVM value) { try { // VALIDATE var validateMessage = await this.ValidateAsync(value); var validateResult = this.GetValue(validateMessage); if (!validateResult) { return(validateMessage.Result); } // NEW MODEL var user = this.GetService <Helpers.User>(); var data = new CategoryData() { ResourceID = user.ResourceID, Text = value.Text, Type = (short)value.Type, ParentID = value.ParentID, RowStatus = 1 }; // HIERARCHY TEXT var parentRow = await this.GetDataQuery().Where(x => x.CategoryID == value.ParentID).FirstOrDefaultAsync(); data.HierarchyText = string.Empty; if (parentRow != null) { data.HierarchyText = $"{parentRow.HierarchyText} / "; } data.HierarchyText += value.Text; // APPLY await this.dbContext.Categories.AddAsync(data); await this.dbContext.SaveChangesAsync(); // RESULT var result = CategoryVM.Convert(data, parentRow); return(this.CreatedResponse("categories", result.CategoryID, result)); } catch (Exception ex) { return(this.ExceptionResponse(ex)); } }
public async Task <ActionResult <CategoryVM> > UpdateAsync(long categoryID, CategoryVM value) { try { // VALIDATE var validateMessage = await this.ValidateAsync(value); var validateResult = this.GetValue(validateMessage); if (!validateResult) { return(validateMessage.Result); } // LOCATE DATA var data = await this.GetDataQuery().Where(x => x.CategoryID == categoryID).FirstOrDefaultAsync(); if (data == null) { return(this.NotFoundResponse()); } // HIERARCHY TEXT var parentRow = await this.GetDataQuery().Where(x => x.CategoryID == value.ParentID).FirstOrDefaultAsync(); data.HierarchyText = string.Empty; if (parentRow != null) { data.HierarchyText = $"{parentRow.HierarchyText} / "; } data.HierarchyText += value.Text; // UPDATE CHILDREN await this.UpdateAsync_Children(data.CategoryID, data.HierarchyText); // APPLY data.Text = value.Text; data.ParentID = value.ParentID; await this.dbContext.SaveChangesAsync(); // RESULT var result = CategoryVM.Convert(data, parentRow); return(this.OkResponse(result)); } catch (Exception ex) { return(this.ExceptionResponse(ex)); } }