mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-03 16:19:46 +00:00
Fixed bug with retrieving deleted data. Added predicates to repositories.
This commit is contained in:
parent
261bd276a8
commit
3f371f68eb
@ -54,8 +54,8 @@ public class UserManager : ServiceBase, IUserManager
|
||||
_logger.LogInformation($"Logging in user with email: {login.Email} and phone: {login.Phone}.");
|
||||
|
||||
var user = string.IsNullOrEmpty(login.Phone)
|
||||
? await this._usersRepository.GetUserAsync(u => u.Email == login.Email && u.IsDeleted == false, cancellationToken)
|
||||
: await this._usersRepository.GetUserAsync(u => u.Phone == login.Phone && u.IsDeleted == false, cancellationToken);
|
||||
? await this._usersRepository.GetUserAsync(u => u.Email == login.Email, cancellationToken)
|
||||
: await this._usersRepository.GetUserAsync(u => u.Phone == login.Phone, cancellationToken);
|
||||
if (user == null)
|
||||
{
|
||||
throw new EntityNotFoundException<User>();
|
||||
@ -79,11 +79,11 @@ public class UserManager : ServiceBase, IUserManager
|
||||
{
|
||||
_logger.LogInformation($"Logging in / Registering guest with guest id: {guest.GuestId}.");
|
||||
|
||||
var user = await this._usersRepository.GetUserAsync(x => x.GuestId == guest.GuestId && x.IsDeleted == false, cancellationToken);
|
||||
var user = await this._usersRepository.GetUserAsync(x => x.GuestId == guest.GuestId, cancellationToken);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
var role = await this._rolesRepository.GetRoleAsync(r => r.Name == "Guest" && r.IsDeleted == false, cancellationToken);
|
||||
var role = await this._rolesRepository.GetRoleAsync(r => r.Name == "Guest", cancellationToken);
|
||||
user = new User
|
||||
{
|
||||
GuestId = guest.GuestId,
|
||||
@ -116,7 +116,7 @@ public class UserManager : ServiceBase, IUserManager
|
||||
.GetOneAsync(r =>
|
||||
r.Token == tokensModel.RefreshToken
|
||||
&& r.CreatedById == userId
|
||||
&& r.IsDeleted == false, cancellationToken);
|
||||
, cancellationToken);
|
||||
if (refreshTokenModel == null || refreshTokenModel.ExpiryDateUTC < DateTime.UtcNow)
|
||||
{
|
||||
throw new SecurityTokenExpiredException();
|
||||
@ -148,7 +148,7 @@ public class UserManager : ServiceBase, IUserManager
|
||||
{
|
||||
_logger.LogInformation($"Adding Role: {roleName} to User with Id: {userId}.");
|
||||
|
||||
var role = await this._rolesRepository.GetRoleAsync(r => r.Name == roleName && r.IsDeleted == false, cancellationToken);
|
||||
var role = await this._rolesRepository.GetRoleAsync(r => r.Name == roleName, cancellationToken);
|
||||
if (role == null)
|
||||
{
|
||||
throw new EntityNotFoundException<Role>();
|
||||
@ -174,7 +174,7 @@ public class UserManager : ServiceBase, IUserManager
|
||||
{
|
||||
_logger.LogInformation($"Removing Role: {roleName} from User with Id: {userId}.");
|
||||
|
||||
var role = await this._rolesRepository.GetRoleAsync(r => r.Name == roleName && r.IsDeleted == false, cancellationToken);
|
||||
var role = await this._rolesRepository.GetRoleAsync(r => r.Name == roleName, cancellationToken);
|
||||
if (role == null)
|
||||
{
|
||||
throw new EntityNotFoundException<Role>();
|
||||
@ -202,7 +202,7 @@ public class UserManager : ServiceBase, IUserManager
|
||||
{
|
||||
_logger.LogInformation($"Updating user with id: {GlobalUser.Id}.");
|
||||
|
||||
var user = await this._usersRepository.GetUserAsync(x => x.Id == GlobalUser.Id && x.IsDeleted == false, cancellationToken);
|
||||
var user = await this._usersRepository.GetUserAsync(x => x.Id == GlobalUser.Id, cancellationToken);
|
||||
if (user == null)
|
||||
{
|
||||
throw new EntityNotFoundException<User>();
|
||||
@ -314,7 +314,7 @@ public class UserManager : ServiceBase, IUserManager
|
||||
{
|
||||
if (!string.IsNullOrEmpty(user.PasswordHash) && (!string.IsNullOrEmpty(user.Email) || !string.IsNullOrEmpty(user.Phone)))
|
||||
{
|
||||
var role = await this._rolesRepository.GetRoleAsync(x => x.Name == "User" && x.IsDeleted == false, cancellationToken);
|
||||
var role = await this._rolesRepository.GetRoleAsync(x => x.Name == "User", cancellationToken);
|
||||
user.Roles.Add(role);
|
||||
}
|
||||
}
|
||||
@ -326,7 +326,7 @@ public class UserManager : ServiceBase, IUserManager
|
||||
{
|
||||
ValidateEmail(userDto.Email);
|
||||
if (userDto.Email != user.Email
|
||||
&& await this._usersRepository.ExistsAsync(x => x.Email == userDto.Email && x.IsDeleted == false, cancellationToken))
|
||||
&& await this._usersRepository.ExistsAsync(x => x.Email == userDto.Email, cancellationToken))
|
||||
{
|
||||
throw new EntityAlreadyExistsException<User>("email", userDto.Email);
|
||||
}
|
||||
@ -336,7 +336,7 @@ public class UserManager : ServiceBase, IUserManager
|
||||
{
|
||||
ValidatePhone(userDto.Phone);
|
||||
if (userDto.Phone != user.Phone
|
||||
&& await this._usersRepository.ExistsAsync(x => x.Phone == userDto.Phone && x.IsDeleted == false, cancellationToken))
|
||||
&& await this._usersRepository.ExistsAsync(x => x.Phone == userDto.Phone, cancellationToken))
|
||||
{
|
||||
throw new EntityAlreadyExistsException<User>("phone", userDto.Phone);
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public class ProductService : IProductService
|
||||
"\n[Products] - return semicolon separated product names";
|
||||
|
||||
var countOfMessage = await _messagesRepository
|
||||
.GetCountAsync(message=>message.WishlistId==ObjectId.Parse((wishlistId)) && message.IsDeleted == false, cancellationToken);
|
||||
.GetCountAsync(message=>message.WishlistId == ObjectId.Parse((wishlistId)), cancellationToken);
|
||||
|
||||
var previousMessages = await _wishlistsService
|
||||
.GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, countOfMessage, cancellationToken);
|
||||
|
@ -24,7 +24,7 @@ public class RolesService : IRolesService
|
||||
|
||||
public async Task<RoleDto> AddRoleAsync(RoleCreateDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var role = await this._repository.GetRoleAsync(r => r.Name == dto.Name && r.IsDeleted == false, cancellationToken);
|
||||
var role = await this._repository.GetRoleAsync(r => r.Name == dto.Name, cancellationToken);
|
||||
if (role != null)
|
||||
{
|
||||
throw new EntityAlreadyExistsException<Role>();
|
||||
|
@ -74,7 +74,7 @@ public class WishlistsService : IWishlistsService
|
||||
var wishlist = await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
|
||||
|
||||
var firstUserMessage =
|
||||
(await _messagesRepository.GetPageAsync(1, 1, x => x.WishlistId == wishlistObjectId && x.Role == MessageRoles.User.ToString() && x.IsDeleted == false, cancellationToken)).First();
|
||||
(await _messagesRepository.GetPageAsync(1, 1, x => x.WishlistId == wishlistObjectId && x.Role == MessageRoles.User.ToString(), cancellationToken)).First();
|
||||
|
||||
var chatCompletionRequest = new ChatCompletionRequest
|
||||
{
|
||||
@ -124,7 +124,7 @@ public class WishlistsService : IWishlistsService
|
||||
|
||||
public async Task<PagedList<WishlistDto>> GetPersonalWishlistsPageAsync(int pageNumber, int pageSize, CancellationToken cancellationToken)
|
||||
{
|
||||
var entities = await _wishlistsRepository.GetPageAsync(pageNumber, pageSize, x => x.CreatedById == GlobalUser.Id && x.IsDeleted == false, cancellationToken);
|
||||
var entities = await _wishlistsRepository.GetPageAsync(pageNumber, pageSize, x => x.CreatedById == GlobalUser.Id, cancellationToken);
|
||||
var dtos = _mapper.Map<List<WishlistDto>>(entities);
|
||||
var count = await _wishlistsRepository.GetTotalCountAsync();
|
||||
return new PagedList<WishlistDto>(dtos, pageNumber, pageSize, count);
|
||||
@ -151,10 +151,10 @@ public class WishlistsService : IWishlistsService
|
||||
|
||||
await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
|
||||
|
||||
var entities = await _messagesRepository.GetPageStartingFromEndAsync(pageNumber, pageSize, x => x.WishlistId == wishlistObjectId && x.IsDeleted == false, cancellationToken);
|
||||
var entities = await _messagesRepository.GetPageStartingFromEndAsync(pageNumber, pageSize, x => x.WishlistId == wishlistObjectId, cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<MessageDto>>(entities);
|
||||
var count = await _messagesRepository.GetCountAsync(x => x.WishlistId == wishlistObjectId && x.IsDeleted == false, cancellationToken);
|
||||
var count = await _messagesRepository.GetCountAsync(x => x.WishlistId == wishlistObjectId, cancellationToken);
|
||||
return new PagedList<MessageDto>(dtos, pageNumber, pageSize, count);
|
||||
}
|
||||
|
||||
@ -187,10 +187,10 @@ public class WishlistsService : IWishlistsService
|
||||
|
||||
await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
|
||||
|
||||
var entities = await _productsRepository.GetPageAsync(pageNumber, pageSize, x => x.WishlistId == wishlistObjectId && x.IsDeleted == false, cancellationToken);
|
||||
var entities = await _productsRepository.GetPageAsync(pageNumber, pageSize, x => x.WishlistId == wishlistObjectId, cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<ProductDto>>(entities);
|
||||
var count = await _productsRepository.GetCountAsync(x => x.WishlistId == wishlistObjectId && x.IsDeleted == false, cancellationToken);
|
||||
var count = await _productsRepository.GetCountAsync(x => x.WishlistId == wishlistObjectId, cancellationToken);
|
||||
return new PagedList<ProductDto>(dtos, pageNumber, pageSize, count);
|
||||
}
|
||||
|
||||
@ -213,7 +213,7 @@ public class WishlistsService : IWishlistsService
|
||||
|
||||
private async Task<Wishlist> TryGetPersonalWishlist(ObjectId wishlistId, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _wishlistsRepository.GetWishlistAsync(x => x.Id == wishlistId && x.IsDeleted == false, cancellationToken);
|
||||
var entity = await _wishlistsRepository.GetWishlistAsync(x => x.Id == wishlistId, cancellationToken);
|
||||
|
||||
if (entity.CreatedById != GlobalUser.Id)
|
||||
{
|
||||
|
@ -41,9 +41,9 @@ public class DbInitialaizer
|
||||
|
||||
public async Task AddUsers(CancellationToken cancellationToken)
|
||||
{
|
||||
var userRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("User") && x.IsDeleted == false)).FirstAsync();
|
||||
var guestRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("Guest") && x.IsDeleted == false)).FirstAsync();
|
||||
var adminRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("Admin") && x.IsDeleted == false)).FirstAsync();
|
||||
var userRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("User"))).FirstAsync();
|
||||
var guestRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("Guest"))).FirstAsync();
|
||||
var adminRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("Admin"))).FirstAsync();
|
||||
|
||||
var users = new User[]
|
||||
{
|
||||
@ -179,8 +179,8 @@ public class DbInitialaizer
|
||||
|
||||
public async Task AddWishlistsWithMessages(CancellationToken cancellationToken)
|
||||
{
|
||||
var user1 = await (await _userCollection.FindAsync(x => x.Email.Equals("shopping.assistant.team@gmail.com") && x.IsDeleted == false)).FirstAsync();
|
||||
var user2 = await (await _userCollection.FindAsync(x => x.Email.Equals("mykhailo.bilodid@nure.ua") && x.IsDeleted == false)).FirstAsync();
|
||||
var user1 = await (await _userCollection.FindAsync(x => x.Email.Equals("shopping.assistant.team@gmail.com"))).FirstAsync();
|
||||
var user2 = await (await _userCollection.FindAsync(x => x.Email.Equals("mykhailo.bilodid@nure.ua"))).FirstAsync();
|
||||
|
||||
var wishlists = new Wishlist[]
|
||||
{
|
||||
|
@ -3,6 +3,7 @@ using MongoDB.Driver;
|
||||
using ShoppingAssistantApi.Application.IRepositories;
|
||||
using ShoppingAssistantApi.Domain.Common;
|
||||
using ShoppingAssistantApi.Persistance.Database;
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace ShoppingAssistantApi.Persistance.Repositories;
|
||||
@ -26,7 +27,8 @@ public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where T
|
||||
|
||||
public async Task<TEntity> GetOneAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
return await this._collection.Find(predicate).FirstOrDefaultAsync(cancellationToken);
|
||||
return await this._collection.Find(Builders<TEntity>.Filter.Where(predicate) & Builders<TEntity>.Filter.Where(x => !x.IsDeleted))
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken)
|
||||
@ -37,7 +39,7 @@ public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where T
|
||||
|
||||
public async Task<List<TEntity>> GetPageAsync(int pageNumber, int pageSize, CancellationToken cancellationToken)
|
||||
{
|
||||
return await this._collection.Find(Builders<TEntity>.Filter.Empty)
|
||||
return await this._collection.Find(Builders<TEntity>.Filter.Where(x => !x.IsDeleted))
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Limit(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
@ -45,7 +47,7 @@ public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where T
|
||||
|
||||
public async Task<List<TEntity>> GetPageAsync(int pageNumber, int pageSize, Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
return await this._collection.Find(predicate)
|
||||
return await this._collection.Find(Builders<TEntity>.Filter.Where(predicate) & Builders<TEntity>.Filter.Where(x => !x.IsDeleted))
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Limit(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
@ -58,12 +60,12 @@ public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where T
|
||||
|
||||
public async Task<int> GetCountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
return (int)(await this._collection.CountDocumentsAsync(predicate, cancellationToken: cancellationToken));
|
||||
return (int)(await this._collection.CountDocumentsAsync(Builders<TEntity>.Filter.Where(predicate) & Builders<TEntity>.Filter.Where(x => !x.IsDeleted), cancellationToken: cancellationToken));
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
return await this._collection.Find(predicate).AnyAsync(cancellationToken);
|
||||
return await this._collection.Find(Builders<TEntity>.Filter.Where(predicate) & Builders<TEntity>.Filter.Where(x => !x.IsDeleted)).AnyAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<TEntity> DeleteAsync(TEntity entity, CancellationToken cancellationToken)
|
||||
|
@ -12,7 +12,7 @@ public class MessagesRepository : BaseRepository<Message>, IMessagesRepository
|
||||
|
||||
public async Task<List<Message>> GetPageStartingFromEndAsync(int pageNumber, int pageSize, Expression<Func<Message, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _collection.Find(predicate)
|
||||
return await _collection.Find(Builders<Message>.Filter.Where(predicate) & Builders<Message>.Filter.Where(x => !x.IsDeleted))
|
||||
.SortByDescending(x => x.CreatedDateUtc)
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Limit(pageSize)
|
||||
|
@ -18,6 +18,6 @@ public class RolesRepository : BaseRepository<Role>, IRolesRepository
|
||||
|
||||
public async Task<Role> GetRoleAsync(Expression<Func<Role, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
return await (await this._collection.FindAsync(predicate)).FirstOrDefaultAsync(cancellationToken);
|
||||
return await (await this._collection.FindAsync(Builders<Role>.Filter.Where(predicate) & Builders<Role>.Filter.Where(x => !x.IsDeleted))).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ public class UsersRepository : BaseRepository<User>, IUsersRepository
|
||||
|
||||
public async Task<User> GetUserAsync(Expression<Func<User, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
return await (await this._collection.FindAsync(predicate)).FirstOrDefaultAsync(cancellationToken);
|
||||
return await (await this._collection.FindAsync(Builders<User>.Filter.Where(predicate) & Builders<User>.Filter.Where(x => !x.IsDeleted))).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<User> UpdateUserAsync(User user, CancellationToken cancellationToken)
|
||||
@ -39,6 +39,6 @@ public class UsersRepository : BaseRepository<User>, IUsersRepository
|
||||
};
|
||||
|
||||
return await this._collection.FindOneAndUpdateAsync(
|
||||
Builders<User>.Filter.Eq(u => u.Id, user.Id), updateDefinition, options, cancellationToken);
|
||||
Builders<User>.Filter.Eq(u => u.Id, user.Id) & Builders<User>.Filter.Where(x => !x.IsDeleted), updateDefinition, options, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,13 @@ public class WishlistsRepository : BaseRepository<Wishlist>, IWishlistsRepositor
|
||||
|
||||
public async Task<Wishlist> GetWishlistAsync(Expression<Func<Wishlist, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
return await (await _collection.FindAsync(predicate)).FirstOrDefaultAsync(cancellationToken);
|
||||
return await (await _collection.FindAsync(Builders<Wishlist>.Filter.Where(predicate) & Builders<Wishlist>.Filter.Where(x => !x.IsDeleted))).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Wishlist> UpdateWishlistNameAsync(ObjectId wishlistId, string newName,
|
||||
ObjectId updatedById, CancellationToken cancellationToken)
|
||||
{
|
||||
var filterDefinition = Builders<Wishlist>.Filter.Eq(w => w.Id, wishlistId);
|
||||
var filterDefinition = Builders<Wishlist>.Filter.Eq(w => w.Id, wishlistId) & Builders<Wishlist>.Filter.Where(x => !x.IsDeleted);
|
||||
|
||||
var updateDefinition = Builders<Wishlist>.Update
|
||||
.Set(w => w.Name, newName)
|
||||
|
Loading…
Reference in New Issue
Block a user