add wishlist and message repositories and wishlist service

This commit is contained in:
cuqmbr 2023-10-08 20:22:53 +03:00
parent 8b78292ce7
commit 7c6d36122d
Signed by: cuqmbr
GPG Key ID: 2D72ED98B6CB200F
9 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,11 @@
using MongoDB.Bson;
using ShoppingAssistantApi.Domain.Common;
namespace ShoppingAssistantApi.Application.Exceptions;
public class UnAuthorizedException<TEntity> : Exception where TEntity : EntityBase
{
public UnAuthorizedException() { }
public UnAuthorizedException(ObjectId id) : base(String.Format($"Access to object {id} of type {typeof(TEntity).Name} denied.")) { }
}

View File

@ -0,0 +1,5 @@
using ShoppingAssistantApi.Domain.Entities;
namespace ShoppingAssistantApi.Application.IRepositories;
public interface IMessagesRepository : IBaseRepository<Message> { }

View File

@ -0,0 +1,5 @@
using ShoppingAssistantApi.Domain.Entities;
namespace ShoppingAssistantApi.Application.IRepositories;
public interface IWishlistsRepository : IBaseRepository<Wishlist> { }

View File

@ -0,0 +1,11 @@
using ShoppingAssistantApi.Application.Models.CreateDtos;
using ShoppingAssistantApi.Application.Models.Dtos;
namespace ShoppingAssistantApi.Application.IServices;
public interface IWishlistsService
{
Task<WishlistDto> StartPersonalWishlistAsync(WishlistCreateDto dto, CancellationToken cancellationToken);
Task<MessageDto> AddMessageToPersonalWishlistAsync(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken);
}

View File

@ -14,6 +14,7 @@ public static class ServicesExtention
services.AddScoped<IUserManager, UserManager>();
services.AddScoped<ITokensService, TokensService>();
services.AddScoped<IUsersService, UsersService>();
services.AddScoped<IWishlistsService, WishlistsService>();
return services;
}

View File

@ -0,0 +1,74 @@
using AutoMapper;
using MongoDB.Bson;
using ShoppingAssistantApi.Application.Exceptions;
using ShoppingAssistantApi.Application.GlobalInstances;
using ShoppingAssistantApi.Application.IRepositories;
using ShoppingAssistantApi.Application.IServices;
using ShoppingAssistantApi.Application.Models.CreateDtos;
using ShoppingAssistantApi.Application.Models.Dtos;
using ShoppingAssistantApi.Application.Paging;
using ShoppingAssistantApi.Domain.Entities;
using ShoppingAssistantApi.Domain.Enums;
namespace ShoppingAssistantApi.Infrastructure.Services;
public class WishlistsService : IWishlistsService
{
private readonly IWishlistsRepository _wishlistsRepository;
private readonly IMessagesRepository _messagesRepository;
private readonly IMapper _mapper;
public WishlistsService(IWishlistsRepository wishlistRepository, IMessagesRepository messageRepository, IMapper mapper)
{
_wishlistsRepository = wishlistRepository;
_messagesRepository = messageRepository;
_mapper = mapper;
}
public async Task<WishlistDto> StartPersonalWishlistAsync(WishlistCreateDto dto, CancellationToken cancellationToken)
{
var newWishlist = _mapper.Map<Wishlist>(dto);
newWishlist.CreatedById = (ObjectId) GlobalUser.Id;
newWishlist.CreatedDateUtc = DateTime.UtcNow;
newWishlist.Name = $"{newWishlist.Type} Search";
var createdWishlist = await _wishlistsRepository.AddAsync(newWishlist, cancellationToken);
var newMessage = new Message
{
Text = dto.FirstMessageText,
Role = MessageRoles.User.ToString(),
WishlistId = createdWishlist.Id
};
var createdMessage = await _messagesRepository.AddAsync(newMessage, cancellationToken);
return _mapper.Map<WishlistDto>(createdWishlist);
}
public async Task<MessageDto> AddMessageToPersonalWishlistAsync(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken)
{
var newMessage = _mapper.Map<Message>(dto);
if (!ObjectId.TryParse(wishlistId, out var wishlistObjectId))
{
throw new InvalidDataException("Provided id is invalid.");
}
newMessage.WishlistId = wishlistObjectId;
newMessage.Role = MessageRoles.User.ToString();
newMessage.CreatedById = (ObjectId) GlobalUser.Id;
newMessage.CreatedDateUtc = DateTime.UtcNow;
var relatedWishlistPage = await _wishlistsRepository.GetPageAsync(1, 1, x => x.Id == wishlistObjectId && x.CreatedById == GlobalUser.Id, cancellationToken);
var relatedWishlist = relatedWishlistPage.FirstOrDefault();
if (relatedWishlist == null)
{
throw new UnAuthorizedException<Wishlist>();
}
var createdMessage = await _messagesRepository.AddAsync(newMessage, cancellationToken);
return _mapper.Map<MessageDto>(createdMessage);
}
}

View File

@ -13,6 +13,8 @@ public static class RepositoriesExtention
services.AddScoped<IRolesRepository, RolesRepository>();
services.AddScoped<IUsersRepository, UsersRepository>();
services.AddScoped<IWishlistsRepository, WishlistsRepository>();
services.AddScoped<IMessagesRepository, MessagesRepository>();
return services;
}

View File

@ -0,0 +1,10 @@
using ShoppingAssistantApi.Application.IRepositories;
using ShoppingAssistantApi.Domain.Entities;
using ShoppingAssistantApi.Persistance.Database;
namespace ShoppingAssistantApi.Persistance.Repositories;
public class MessagesRepository : BaseRepository<Message>, IMessagesRepository
{
public MessagesRepository(MongoDbContext db) : base(db, "Messages") { }
}

View File

@ -0,0 +1,10 @@
using ShoppingAssistantApi.Application.IRepositories;
using ShoppingAssistantApi.Domain.Entities;
using ShoppingAssistantApi.Persistance.Database;
namespace ShoppingAssistantApi.Persistance.Repositories;
public class WishlistsRepository : BaseRepository<Wishlist>, IWishlistsRepository
{
public WishlistsRepository(MongoDbContext db) : base(db, "Wishlists") { }
}