public async Task <Unit> Handle(CreateUserCommand request, CancellationToken cancellationToken) { var userExists = await _context.Users .AnyAsync(x => x.Email.ToLower().Equals(request.Email.ToLower())); if (userExists) { throw new ConflictException($"User with email: {request.Email} already exists."); } var user = new User { Password = AuthHelper.GetPasswordHash(request.Password), PhoneNumber = request.PhoneNumber, Email = request.Email, FirstName = request.FirstName, LastName = request.LastName, RoleId = 1 }; await _context.Users.AddAsync(user, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken) { var user = await _context.Users .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (user == null) { throw new NotFoundException($"User id {request.Id} not exists."); } if (_currentUserService.UserId != user.Id && !_currentUserService.Role.Equals("Admin")) { throw new UnauthorizedException("You can't do this action."); } user.FirstName = request.FirstName; user.LastName = request.LastName; user.PhoneNumber = request.PhoneNumber; if (_currentUserService.Role.Equals("Admin")) { user.RoleId = request.RoleId; } await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <CreateCommentCommandResponse> Handle(CreateCommentCommand request, CancellationToken cancellationToken) { var userExists = await _context.Users.AnyAsync(x => x.Id == request.UserId); if (!userExists) { throw new NotFoundException($"User was not found with specified id = {request.UserId}"); } if (_currentUserService.UserId != request.UserId) { throw new UnauthorizedException("You can't do this action."); } var petExists = await _context.Pets.AnyAsync(x => x.Id == request.PetId); if (!petExists) { throw new NotFoundException($"Pet with specified id = {request.PetId} not exist."); } var comment = new Comment { Text = request.Text, PetId = request.PetId, CreatedAt = DateTime.Now, CreatedByUserId = request.UserId }; await _context.Comments.AddAsync(comment, cancellationToken); await _context.SaveChangesAsync(cancellationToken); var user = await _context.Users.FindAsync(comment.CreatedByUserId); return(new CreateCommentCommandResponse { Id = comment.Id, CreatedAt = comment.CreatedAt, Text = comment.Text, CreatedByUser = new UserDto { Id = user.Id, Email = user.Email, FirstName = user.FirstName, LastName = user.LastName, PhoneNumber = user.PhoneNumber, RoleId = user.RoleId } }); }
public async Task <Unit> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken) { var category = await _context.Categories .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (category == null) { throw new NotFoundException($"Category id {request.Id} not exists."); } _context.Categories.Remove(category); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken) { var user = await _context.Users .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (user == null) { throw new NotFoundException($"User id {request.Id} not exists."); } _context.Users.Remove(user); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CreateLikeCommand request, CancellationToken cancellationToken) { var userExists = await _context.Users.AnyAsync(x => x.Id == request.UserId); if (!userExists) { throw new NotFoundException($"User was not found with specified id = {request.UserId}"); } if (_currentUserService.UserId != request.UserId) { throw new UnauthorizedException("You can't do this action."); } var petExists = await _context.Pets.AnyAsync(x => x.Id == request.PetId); if (!petExists) { throw new NotFoundException($"Pet was not found with specified id = {request.PetId}"); } var like = await _context.Likes .FirstOrDefaultAsync(x => x.UserId == request.UserId && x.PetId == request.PetId, cancellationToken); if (like == null) { var newlyCreatedLike = new Like { PetId = request.PetId, UserId = request.UserId }; await _context.Likes.AddAsync(newlyCreatedLike, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); } else { throw new ConflictException("This pet is already liked."); } }
public async Task <Unit> Handle(CreateCategoryCommand request, CancellationToken cancellationToken) { var categoryWithNameExist = await _context.Categories .AnyAsync(x => x.Title.ToLower().Equals(request.Title.ToLower())); if (categoryWithNameExist) { throw new ConflictException($"Category with name {request.Title} already exists."); } await _context.Categories.AddAsync(new Category { Title = request.Title }, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeletePetCommand request, CancellationToken cancellationToken) { var pet = await _context.Pets .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (pet == null) { throw new NotFoundException($"Pet id {request.Id} not exists."); } if (_currentUserService.UserId != pet.UserId && !_currentUserService.Role.Equals("Admin")) { throw new UnauthorizedException("You can't do this action."); } _context.Pets.Remove(pet); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(UpdateCommentCommand request, CancellationToken cancellationToken) { if (_currentUserService.UserId != request.UserId && !_currentUserService.Role.Equals("Admin")) { throw new UnauthorizedException("You can't do this action."); } var comment = await _context.Comments .FirstOrDefaultAsync(x => x.Id == request.Id && x.CreatedByUserId == request.UserId, cancellationToken); if (comment == null) { throw new NotFoundException($"Comment id {request.Id} not exists."); } comment.Text = request.Text; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(UpdatePetCommand request, CancellationToken cancellationToken) { var pet = await _context.Pets .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (pet == null) { throw new NotFoundException($"Pet id {request.Id} not exists."); } if (_currentUserService.UserId != request.UserId && !_currentUserService.Role.Equals("Admin")) { throw new UnauthorizedException("You can't do this action."); } var categoryExists = await _context.Categories.AnyAsync(x => x.Id == request.CategoryId); if (!categoryExists) { throw new NotFoundException($"User was not found with specified id = {request.CategoryId}"); } pet.Name = request.Name; pet.Age = request.Age; pet.City = request.City; pet.Gender = request.Gender; pet.Weight = request.Weight; pet.Height = request.Height; pet.IsSterilized = request.IsSterilized; pet.Description = request.Description; pet.PhotoCode = request.PhotoCode; pet.CategoryId = request.CategoryId; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CreatePetCommand request, CancellationToken cancellationToken) { if (_currentUserService.UserId != request.UserId) { throw new UnauthorizedException("You can't do this action."); } var categoryExists = await _context.Categories.AnyAsync(x => x.Id == request.CategoryId); if (!categoryExists) { throw new NotFoundException($"User was not found with specified id = {request.CategoryId}"); } Pet pet = new Pet { Name = request.Name, Age = request.Age, City = request.City, Gender = request.Gender, Weight = request.Weight, Height = request.Height, IsSterilized = request.IsSterilized, Description = request.Description, DateAdded = DateTime.Now, State = State.NotGivenAway, CategoryId = request.CategoryId, PhotoCode = request.PhotoCode, UserId = request.UserId }; await _context.Pets.AddAsync(pet, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) { var category = await _context.Categories .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (category == null) { throw new NotFoundException($"Category id {request.Id} not exists."); } var categoryWithNameExist = await _context.Categories .AnyAsync(x => x.Title.ToLower().Equals(request.Title.ToLower())); if (categoryWithNameExist) { throw new ConflictException($"Category with name {request.Title} already exists."); } category.Title = request.Title; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeleteLikeCommand request, CancellationToken cancellationToken) { var userExists = await _context.Users.AnyAsync(x => x.Id == request.UserId); if (!userExists) { throw new NotFoundException($"User was not found with specified id = {request.UserId}"); } if (_currentUserService.UserId != request.UserId) { throw new UnauthorizedException("You can't do this action."); } var petExists = await _context.Pets.AnyAsync(x => x.Id == request.PetId); if (!petExists) { throw new NotFoundException($"Pet was not found with specified id = {request.PetId}"); } var like = await _context.Likes .FirstOrDefaultAsync(x => x.UserId == request.UserId && x.PetId == request.PetId, cancellationToken); if (like == null) { throw new NotFoundException("Pet can't be unliked because it is not liked."); } _context.Likes.Remove(like); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }