public async Task <IActionResult> Get(Guid?id) { var serviceModel = new GetCategoryServiceModel { Id = id, Language = CultureInfo.CurrentCulture.Name }; var validator = new GetCategoryModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { var category = await this.categoryService.GetAsync(serviceModel); if (category != null) { var response = new CategoryResponseModel { Id = category.Id, IsLeaf = category.IsLeaf, Level = category.Level, Name = category.Name, Order = category.Order, ParentCategoryName = category.ParentCategoryName, ParentId = category.ParentId, ThumbnailMediaId = category.ThumbnailMediaId, LastModifiedDate = category.LastModifiedDate, CreatedDate = category.CreatedDate }; return(this.StatusCode((int)HttpStatusCode.OK, response)); } else { return(this.StatusCode((int)HttpStatusCode.NotFound)); } } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); }
public async Task <CategoryServiceModel> GetAsync(GetCategoryServiceModel model) { var categories = from c in this.context.Categories join t in this.context.CategoryTranslations on c.Id equals t.CategoryId into ct from x in ct.DefaultIfEmpty() join m in this.context.CategoryImages on c.Id equals m.CategoryId into cm from y in cm.DefaultIfEmpty() where x.Language == model.Language && c.Id == model.Id && c.IsActive orderby c.Order select new CategoryServiceModel { Id = c.Id, Order = c.Order, Level = c.Level, IsLeaf = c.IsLeaf, ParentId = c.Parentid, Name = x.Name, ThumbnailMediaId = y.MediaId }; return(await categories.FirstOrDefaultAsync()); }