(feat): Category controllers

This commit is contained in:
Maksym Sadovnychyy 2022-08-22 23:04:00 +02:00
parent b635c3bc3b
commit 4d0133ada9
20 changed files with 351 additions and 189 deletions

View File

@ -1,7 +1,7 @@
using Core.Abstractions.DomainObjects;
namespace Core.DomainObjects.Documents {
public class BlogItem : PostItemBase<BlogItem> {
public class BlogItem : PostItemBase<BlogItem> {
public uint? ReadTime { get; set; }

View File

@ -38,7 +38,7 @@ namespace DataProviders {
public (BlogItem?, IDomainResult) Get(Guid siteId, string slug) {
var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.L10n.Where(y => y.Slug == slug).Count() > 0, _collectionName);
if (!result.IsSuccess || list == null || list.Count == 0)
if (!result.IsSuccess || list == null)
return (null, result);
return (list.First(), result);

View File

@ -1,21 +1,25 @@
using Core.DomainObjects;
using Core.Enumerations;
using DataProviders.Abstractions;
using DomainResults.Common;
using Microsoft.Extensions.Logging;
using MongoDB.Bson.Serialization;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization;
using DomainResults.Common;
using DataProviders.Abstractions;
using Core.DomainObjects;
namespace DataProviders {
public interface ICategoryDataProvider {
(Category?, IDomainResult) Get(Guid id, Guid siteId);
(Guid?, IDomainResult) Insert(Category obj);
(Category?, IDomainResult) Get(Guid siteId, Guid categoryId);
(Category?, IDomainResult) Get(Guid siteId, List<string> slugs);
(List<Category>?, IDomainResult) GetAll(Guid siteId);
(Guid?, IDomainResult) Update(Category obj);
IDomainResult Delete(Guid id);
IDomainResult DeleteAll(Guid siteId);
}
public class CategoryDataProvider : DataProviderBase<Category>, ICategoryDataProvider {
@ -28,13 +32,46 @@ namespace DataProviders {
ISessionService sessionService) : base(logger, client, idGenerator, sessionService) {
}
public (Category?, IDomainResult) Get(Guid id, Guid siteId) {
var (list, result) = GetWithPredicate(x => x.Id == id && x.SiteId == siteId, 0, 0, _collectionName);
public (Guid?, IDomainResult) Insert(Category obj) =>
Insert(obj, _collectionName);
if (!result.IsSuccess || list == null || list.Count == 0)
public (Category?, IDomainResult) Get(Guid siteId, Guid categoryId) {
var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.Id == categoryId, _collectionName);
if (!result.IsSuccess || list == null)
return (null, result);
return (list.First(), result);
}
public (Category?, IDomainResult) Get(Guid siteId, List<string> slugs) {
var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.L10n.Any(y => slugs.Contains(y.Slug)), _collectionName);
if (!result.IsSuccess || list == null)
return (null, result);
return (list.First(), result);
}
public (Category?, IDomainResult) Get(Guid siteId, string slug) {
var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.L10n.Where(x => x.Slug == slug).Count() > 0 , _collectionName);
if (!result.IsSuccess || list == null)
return (null, result);
return (list.First(), result);
}
public (List<Category>?, IDomainResult) GetAll(Guid siteId) =>
GetWithPredicate(x => x.SiteId == siteId, _collectionName);
public (Guid?, IDomainResult) Update(Category obj) =>
UpdateWithPredicate(obj, x => x.Id == obj.Id, _collectionName);
public IDomainResult Delete(Guid id) =>
DeleteWithPredicate(x => x.Id == id, _collectionName);
public IDomainResult DeleteAll(Guid siteId) =>
DeleteWithPredicate(x => x.SiteId == siteId, _collectionName);
}
}

View File

@ -25,7 +25,7 @@ namespace DataProviders {
var (list, result) = GetWithPredicate(x => x.SiteId == siteId
&& (x.Localization.Locale == null || x.Localization.Locale.ToLower() == locale.ToLower()), 0, 0, _collectionName);
if (!result.IsSuccess || list == null || list.Count == 0)
if (!result.IsSuccess || list == null)
return (null, result);
return (list.First(), result);

View File

@ -36,7 +36,7 @@ namespace DataProviders {
public (ShopCartItem?, IDomainResult) Get(Guid siteId, Guid userId, string sku) {
var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.UserId == userId && x.Sku == sku, 0, 0, _collectionName);
if (!result.IsSuccess || list == null || list.Count == 0)
if (!result.IsSuccess || list == null)
return (null, result);
return (list.First(), result);

View File

@ -35,7 +35,7 @@ namespace DataProviders {
public (ShopItem?, IDomainResult) Get(Guid siteId, string sku) {
var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.Sku == sku, _collectionName);
if (!result.IsSuccess || list == null || list.Count == 0)
if (!result.IsSuccess || list == null)
return (null, result);
return (list.First(), result);

View File

@ -1,44 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using WeatherForecast.Models;
using WeatherForecast.Models.Responses;
namespace WeatherForecast.Controllers {
[AllowAnonymous]
[ApiController]
[Route("api/[controller]")]
public class BlogCategoriesController : ControllerBase {
private readonly ILogger<BlogCategoriesController> _logger;
public BlogCategoriesController(ILogger<BlogCategoriesController> logger) {
_logger = logger;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
//[HttpGet]
//public IActionResult Get() {
// var categories = new List<CategoryModel> {
// new CategoryModel {
// Id = Guid.NewGuid(),
// Text = "Software"
// },
// new CategoryModel {
// Id = Guid.NewGuid(),
// Text = "Hardware"
// },
// };
// var response = new GetBlogCategoriesResponseModel {
// Items = categories
// };
// return Ok(response);
//}
}
}

View File

@ -1,30 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using WeatherForecast.Models;
using WeatherForecast.Models.Responses;
namespace WeatherForecast.Controllers {
[AllowAnonymous]
[ApiController]
[Route("api/[controller]")]
public class BlogFeaturedController : ControllerBase {
private readonly ILogger<BlogFeaturedController> _logger;
public BlogFeaturedController(ILogger<BlogFeaturedController> logger) {
_logger = logger;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Get() {
return Ok();
}
}
}

View File

@ -0,0 +1,49 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using WeatherForecast.Services;
using DomainResults.Mvc;
using WeatherForecast.Models.Requests;
namespace WeatherForecast.Controllers {
[AllowAnonymous]
[ApiController]
[Route("api/[controller]")]
public class CategoryItemController : ControllerBase {
private readonly ILogger<CategoryItemController> _logger;
private readonly ICategoryItemService _categoryItemService;
public CategoryItemController(
ILogger<CategoryItemController> logger,
ICategoryItemService categoryItemService) {
_logger = logger;
_categoryItemService = categoryItemService;
}
/// <summary>
///
/// </summary>
/// <param name="siteId"></param>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("{siteId}")]
public IActionResult Get([FromRoute] Guid siteId, [FromBody] PostCategoryItemRequestModel requestData) {
var result = _categoryItemService.Post(siteId, requestData);
return result.ToActionResult();
}
/// <summary>
///
/// </summary>
/// <param name="siteId"></param>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{siteId}/{categoryId}")]
public IActionResult Get([FromRoute] Guid siteId, [FromRoute] Guid categoryId) {
var result = _categoryItemService.Get(siteId, categoryId);
return result.ToActionResult();
}
}
}

View File

@ -0,0 +1,56 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using DomainResults.Mvc;
using Core.Abstractions;
using Core.Enumerations;
using WeatherForecast.Services;
namespace WeatherForecast.Controllers {
[AllowAnonymous]
[ApiController]
[Route("api/[controller]")]
public class CategoryItemsController : ControllerBase {
private readonly ILogger<CategoryItemsController> _logger;
private readonly ICategoryItemsService _categoryItemsService;
public CategoryItemsController(
ILogger<CategoryItemsController> logger,
ICategoryItemsService categoryItemsService
) {
_logger = logger;
_categoryItemsService = categoryItemsService;
}
/// <summary>
///
/// </summary>
/// <param name="siteId"></param>
/// <param name="locale"></param>
/// <returns></returns>
[HttpGet("{siteId}")]
public IActionResult Get([FromRoute] Guid siteId, [FromQuery] string? locale) {
var result = _categoryItemsService.Get(siteId, locale != null
? Enumeration.FromDisplayName<Locales>(locale)
: Locales.Us);
return result.ToActionResult();
}
/// <summary>
///
/// </summary>
/// <param name="siteId"></param>
/// <returns></returns>
[HttpDelete("{siteId}")]
public IActionResult Delete([FromRoute] Guid siteId) {
var result = _categoryItemsService.Delete(siteId);
return result.ToActionResult();
}
}
}

View File

@ -1,39 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using WeatherForecast.Models;
namespace WeatherForecast.Controllers {
[AllowAnonymous]
[ApiController]
[Route("api/[controller]")]
public class ShopCategoriesController : ControllerBase {
private readonly ILogger<ShopCategoriesController> _logger;
public ShopCategoriesController(ILogger<ShopCategoriesController> logger) {
_logger = logger;
}
///// <summary>
/////
///// </summary>
///// <returns></returns>
//[HttpGet]
//public IActionResult Get() {
// var categories = new List<CategoryModel> {
// new CategoryModel {
// Id = Guid.NewGuid(),
// Text = "Software"
// },
// new CategoryModel {
// Id = Guid.NewGuid(),
// Text = "Hardware"
// },
// };
// return Ok(categories);
//}
}
}

View File

@ -1,26 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
namespace WeatherForecast.Controllers {
[AllowAnonymous]
[ApiController]
[Route("api/[controller]")]
public class ShopFeaturedController : ControllerBase {
private readonly ILogger<ShopFeaturedController> _logger;
public ShopFeaturedController(ILogger<ShopFeaturedController> logger) {
_logger = logger;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Get() {
return Ok();
}
}
}

View File

@ -1,29 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
namespace WeatherForecast.Controllers {
[AllowAnonymous]
[ApiController]
[Route("api/[controller]")]
public class ShopRelatedController : ControllerBase {
private readonly ILogger<ShopRelatedController> _logger;
public ShopRelatedController(ILogger<ShopRelatedController> logger) {
_logger = logger;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Get() {
return Ok();
}
}
}

View File

@ -0,0 +1,19 @@
using Core.Abstractions;
using Core.Abstractions.Models;
using Core.DomainObjects.L10n;
using Core.Enumerations;
namespace WeatherForecast.Models.L10n {
public class CategoryL10nModel : RequestModelBase<CategoryL10n> {
public string Locale { get; set; }
public string Slug { get; set; }
public string Text { get; set; }
public override CategoryL10n ToDomainObject() => new CategoryL10n {
Locale = Enumeration.FromDisplayName<Locales>(Locale),
Slug = Slug,
Text = Text
};
}
}

View File

@ -0,0 +1,13 @@
using Core.Abstractions.Models;
using Core.DomainObjects;
using WeatherForecast.Models.L10n;
namespace WeatherForecast.Models.Requests {
public class PostCategoryItemRequestModel : RequestModelBase<Category> {
public List<CategoryL10nModel> L10n { get; set; }
public override Category ToDomainObject() {
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,10 @@
using Core.Abstractions.Models;
using Core.DomainObjects;
namespace WeatherForecast.Models.Requests {
public class PutCategoryItemRequestModel : RequestModelBase<Category> {
public override Category ToDomainObject() {
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,19 @@
using Core.Abstractions.Models;
using Core.DomainObjects;
using Core.Enumerations;
namespace WeatherForecast.Models.Responses {
public class GetCategoryItemResponseModel : ResponseModelBase {
public string Slug { get; set; }
public string Text { get; set; }
public GetCategoryItemResponseModel(Category category, Locales locale) {
var l10n = category.L10n.SingleOrDefault(x => x.Locale == locale);
if (l10n != null) {
Slug = l10n.Slug;
Text = l10n.Text;
}
}
}
}

View File

@ -0,0 +1,84 @@
using Core.Enumerations;
using DataProviders;
using DomainResults.Common;
using WeatherForecast.Models.Requests;
using WeatherForecast.Models.Responses;
namespace WeatherForecast.Services {
public interface ICategoryItemService {
(Guid?, IDomainResult) Post(Guid siteId, PostCategoryItemRequestModel requestModel);
(GetCategoryItemResponseModel?, IDomainResult) Get(Guid siteId, Guid categoryId);
(Guid?, IDomainResult) Update(Guid siteId, Guid categoryId, PutCategoryItemRequestModel requestData);
IDomainResult Delete(Guid siteId, Guid categoryId);
}
public class CategoryItemService : ICategoryItemService {
ILogger<CategoryItemService> _logger;
ICategoryDataProvider _categoryDataProvider;
public CategoryItemService(
ILogger<CategoryItemService> logger,
ICategoryDataProvider categoryDataProvider
) {
_logger = logger;
_categoryDataProvider = categoryDataProvider;
}
public (Guid?, IDomainResult) Post(Guid siteId, PostCategoryItemRequestModel requestModel) {
var (_, getResult) = _categoryDataProvider.Get(siteId, requestModel.L10n.Select(x => x.Slug).ToList());
if (getResult.IsSuccess)
return IDomainResult.Failed<Guid?>();
var item = requestModel.ToDomainObject();
item.SiteId = siteId;
var (id, insertResult) = _categoryDataProvider.Insert(item);
if (!insertResult.IsSuccess)
return IDomainResult.Failed<Guid?>();
return IDomainResult.Success(id);
}
public (GetCategoryItemResponseModel?, IDomainResult) Get(Guid siteId, Guid categoryId) {
var (item, result) = _categoryDataProvider.Get(siteId, categoryId);
if (!result.IsSuccess || item == null)
return (null, result);
return IDomainResult.Success(new GetCategoryItemResponseModel(item, Locales.Us));
}
public (Guid?, IDomainResult) Update(Guid siteId, Guid categoryId, PutCategoryItemRequestModel requestData) {
var (item, result) = _categoryDataProvider.Get(siteId, categoryId);
if (!result.IsSuccess || item == null)
return (null, result);
// construct domain object from model
var newItem = requestData.ToDomainObject();
newItem.Id = item.Id;
newItem.SiteId = item.SiteId;
if (!item.Equals(newItem)) {
var (id, updateResult) = _categoryDataProvider.Update(newItem);
if (!updateResult.IsSuccess || id == null)
return (null, updateResult);
}
return IDomainResult.Success(item.Id);
}
public IDomainResult Delete(Guid siteId, Guid categoryId) {
var (item, getResult) = _categoryDataProvider.Get(siteId, categoryId);
if (!getResult.IsSuccess || item == null)
return getResult;
var result = _categoryDataProvider.Delete(item.Id);
if (!result.IsSuccess)
return result;
return IDomainResult.Success();
}
}
}

View File

@ -0,0 +1,40 @@
using Core.Enumerations;
using DataProviders;
using DomainResults.Common;
using WeatherForecast.Models.Responses;
namespace WeatherForecast.Services {
public interface ICategoryItemsService {
(List<GetCategoryItemResponseModel>?, IDomainResult) Get(Guid siteId, Locales locale);
IDomainResult Delete(Guid siteId);
}
public class CategoryItemsService : ICategoryItemsService {
private readonly ILogger<CategoryItemsService> _logger;
private readonly ICategoryDataProvider _categoryDataProvider;
public CategoryItemsService(
ILogger<CategoryItemsService> logger,
ICategoryDataProvider categoryDataProvider
) {
_logger = logger;
_categoryDataProvider = categoryDataProvider;
}
public (List<GetCategoryItemResponseModel>?, IDomainResult) Get(Guid siteId, Locales locale) {
var (items, result) = _categoryDataProvider.GetAll(siteId);
if (!result.IsSuccess || items == null)
return (null, result);
return IDomainResult.Success(items.Select(x => new GetCategoryItemResponseModel(x, locale)).ToList());
}
public IDomainResult Delete(Guid siteId) {
var resutl = _categoryDataProvider.DeleteAll(siteId);
return resutl;
}
}
}

View File

@ -64,6 +64,9 @@ namespace WeatherForecast {
services.AddScoped<IShopCartItemsService, ShopCartItemsService>();
//services.AddScoped<IBlogItemService, BlogItemService>();
services.AddScoped<IBlogItemsService, BlogItemsService>();
services.AddScoped<ICategoryItemService, CategoryItemService>();
services.AddScoped<ICategoryItemsService, CategoryItemsService>();
services.RegisterDataproviders(appSettings);
#region Swagger