-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from pedronvasconcelos/development
add: mapping + like a spixer + delete a spixer
- Loading branch information
Showing
34 changed files
with
671 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
CREATE TABLE users ( | ||
user_id UUID PRIMARY KEY, | ||
username VARCHAR(50) NOT NULL, | ||
first_name VARCHAR(50), | ||
last_name VARCHAR(50), | ||
birth_date DATE, | ||
country VARCHAR(50), | ||
city VARCHAR(50), | ||
user_language INT, | ||
email VARCHAR(255) NOT NULL, | ||
biography TEXT, | ||
email_is_verified BOOLEAN, | ||
is_active BOOLEAN, | ||
user_location TEXT, | ||
website VARCHAR(255), | ||
created_at timestamp with time zone, | ||
updated_at timestamp with time zone | ||
); | ||
|
||
CREATE TABLE spixers ( | ||
spixer_id UUID PRIMARY KEY, | ||
content VARCHAR(280), | ||
likes_count INT DEFAULT 0, | ||
created_at timestamp with time zone, | ||
user_id UUID, | ||
active BOOLEAN, | ||
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE | ||
); | ||
|
||
CREATE TABLE spixer_likes ( | ||
like_id UUID PRIMARY KEY, | ||
spixer_id UUID, | ||
user_id UUID, | ||
created_at timestamp WITH TIME ZONE, | ||
FOREIGN KEY (spixer_id) REFERENCES spixers(spixer_id) ON DELETE CASCADE, | ||
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE | ||
); | ||
|
||
CREATE TABLE user_following ( | ||
user_id UUID NOT NULL, | ||
following_id UUID NOT NULL, | ||
|
||
CONSTRAINT pk_user_following PRIMARY KEY (user_id, following_id), | ||
|
||
CONSTRAINT fk_user_following_user | ||
FOREIGN KEY (user_id) | ||
REFERENCES users (user_id) | ||
ON DELETE RESTRICT, | ||
|
||
CONSTRAINT fk_user_following_following | ||
FOREIGN KEY (following_id) | ||
REFERENCES users (user_id) | ||
ON DELETE RESTRICT | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Spix.Application/Spixers/Delete/DeleteSpixerCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Spix.Application.Core; | ||
|
||
namespace Spix.Application.Spixers.Delete; | ||
|
||
public class DeleteSpixerCommand : ICommandBase<DeleteSpixerResponse> | ||
{ | ||
public Guid UserId { get; set; } | ||
public Guid SpixerId { get; set; } | ||
|
||
public DeleteSpixerCommand(Guid userId, Guid spixerId) | ||
{ | ||
UserId = userId; | ||
SpixerId = spixerId; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Spix.Application/Spixers/Delete/DeleteSpixerCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Spix.Application.Core; | ||
using Spix.Domain.Spixers; | ||
|
||
namespace Spix.Application.Spixers.Delete; | ||
|
||
public class DeleteSpixerCommandHandler : ICommandHandlerBase<DeleteSpixerCommand, DeleteSpixerResponse> | ||
{ | ||
private readonly ISpixerRepository _spixerRepository; | ||
|
||
public DeleteSpixerCommandHandler(ISpixerRepository spixerRepository) | ||
{ | ||
_spixerRepository = spixerRepository; | ||
} | ||
|
||
public async Task<ResultBase<DeleteSpixerResponse>> Handle(DeleteSpixerCommand request, CancellationToken cancellationToken) | ||
{ | ||
|
||
var spixer = await _spixerRepository.GetByIdAsync(request.SpixerId); | ||
if (spixer == null) | ||
{ | ||
return ResultBaseFactory.Failure<DeleteSpixerResponse>("Spixer not found"); | ||
} | ||
|
||
if (spixer.UserId != request.UserId) | ||
{ | ||
return ResultBaseFactory.Failure<DeleteSpixerResponse>("User not authorized to delete this spixer"); | ||
} | ||
|
||
spixer.Delete(); | ||
await _spixerRepository.UpdateAsync(spixer); | ||
if (!await _spixerRepository.UnitOfWork.CommitAsync(cancellationToken)) | ||
{ | ||
return ResultBaseFactory.Failure<DeleteSpixerResponse>("Error deleting spixer"); | ||
} | ||
return ResultBaseFactory.Successful(new DeleteSpixerResponse { DeletedAt = DateTime.UtcNow }, "Success"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Spix.Application.Spixers.Delete; | ||
|
||
public class DeleteSpixerResponse | ||
{ | ||
public DateTime DeletedAt { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Spix.Application.Core; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Spix.Application.Spixers.Like; | ||
|
||
public class LikeASpixerCommand : ICommandBase<LikeASpixerResponse> | ||
{ | ||
public Guid UserId { get; set; } | ||
public Guid SpixerId { get; set; } | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Spix.Application/Spixers/Like/LikeASpixerCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Spix.Application.Core; | ||
using Spix.Domain.Likes; | ||
using Spix.Domain.Spixers; | ||
using Spix.Domain.Users; | ||
|
||
namespace Spix.Application.Spixers.Like; | ||
|
||
public class LikeASpixerCommandHandler : ICommandHandlerBase<LikeASpixerCommand, LikeASpixerResponse> | ||
{ | ||
private readonly ISpixerRepository _spixerRepository; | ||
private readonly IUserRepository _userRepository; | ||
|
||
public LikeASpixerCommandHandler(ISpixerRepository spixerRepository, IUserRepository userRepository) | ||
{ | ||
_spixerRepository = spixerRepository; | ||
_userRepository = userRepository; | ||
} | ||
public async Task<ResultBase<LikeASpixerResponse>> Handle(LikeASpixerCommand request, CancellationToken cancellationToken) | ||
{ | ||
|
||
var user = await _userRepository.GetByIdAsync(request.UserId); | ||
if (user == null) | ||
{ | ||
return ResultBaseFactory.Failure<LikeASpixerResponse>("User not found"); | ||
} | ||
var spixer = await _spixerRepository.GetByIdAsync(request.SpixerId); | ||
if (spixer == null) | ||
{ | ||
return ResultBaseFactory.Failure<LikeASpixerResponse>("Spixer not found"); | ||
} | ||
if (await _spixerRepository.IsSpixerLikedByUserAsync(request.SpixerId, request.UserId)) | ||
{ | ||
return ResultBaseFactory.Failure<LikeASpixerResponse>("Spixer already liked"); | ||
} | ||
|
||
var spixerLike = new SpixerLike(spixer.Id, user.Id); | ||
spixer.Like(spixerLike); | ||
|
||
|
||
await _spixerRepository.UpdateAsync(spixer); | ||
await _spixerRepository.AddSpixerLikeAsync(spixerLike); | ||
if (!await _spixerRepository.UnitOfWork.CommitAsync(cancellationToken)) | ||
{ | ||
return ResultBaseFactory.Failure<LikeASpixerResponse>("Error liking spixer"); | ||
} | ||
|
||
|
||
|
||
return ResultBaseFactory.Successful( | ||
new LikeASpixerResponse( | ||
spixerLike.Id, | ||
user.Id, | ||
spixer.Id, | ||
spixerLike.CreatedAt), | ||
"Success"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Spix.Application.Spixers.Like; | ||
|
||
public class LikeASpixerResponse | ||
{ | ||
public Guid Id { get; set; } | ||
public Guid UserId { get; set; } | ||
public Guid SpixerId { get; set; } | ||
public DateTime CreatedAt { get; set; } | ||
|
||
public LikeASpixerResponse(Guid id, Guid userId, Guid spixerId, DateTime createdAt) | ||
{ | ||
Id = id; | ||
UserId = userId; | ||
SpixerId = spixerId; | ||
CreatedAt = createdAt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Spix.Application.Core; | ||
|
||
namespace Spix.Application.Spixers.Unlike; | ||
|
||
public class UnlikeASpixerCommand : ICommandBase<UnlikeASpixerResponse> | ||
{ | ||
public Guid UserId { get; set; } | ||
public Guid SpixerId { get; set; } | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Spix.Application/Spixers/Unlike/UnlikeASpixerCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
| ||
using Spix.Application.Core; | ||
using Spix.Domain.Spixers; | ||
using Spix.Domain.Users; | ||
|
||
namespace Spix.Application.Spixers.Unlike; | ||
|
||
public class UnlikeASpixerCommandHandler : ICommandHandlerBase<UnlikeASpixerCommand, UnlikeASpixerResponse> | ||
{ | ||
private readonly ISpixerRepository _spixerRepository; | ||
private readonly IUserRepository _userRepository; | ||
|
||
public UnlikeASpixerCommandHandler(ISpixerRepository spixerRepository, IUserRepository userRepository) | ||
{ | ||
_spixerRepository = spixerRepository; | ||
_userRepository = userRepository; | ||
} | ||
public async Task<ResultBase<UnlikeASpixerResponse>> Handle(UnlikeASpixerCommand request, CancellationToken cancellationToken) | ||
{ | ||
|
||
var user = await _userRepository.GetByIdAsync(request.UserId); | ||
if (user == null) | ||
{ | ||
return ResultBaseFactory.Failure<UnlikeASpixerResponse>("User not found"); | ||
} | ||
var spixer = await _spixerRepository.GetByIdAsync(request.SpixerId); | ||
if (spixer == null) | ||
{ | ||
return ResultBaseFactory.Failure<UnlikeASpixerResponse>("Spixer not found"); | ||
} | ||
|
||
var like = await _spixerRepository.GetSpixerLikeAsync(request.SpixerId, request.UserId); | ||
if(like == null) | ||
{ | ||
return ResultBaseFactory.Failure<UnlikeASpixerResponse>("Spixer not liked"); | ||
} | ||
|
||
spixer.Unlike(like); | ||
await _spixerRepository.DeleteSpixerLikeAsync(like); | ||
|
||
|
||
await _spixerRepository.UpdateAsync(spixer); | ||
if (!await _spixerRepository.UnitOfWork.CommitAsync(cancellationToken)) | ||
{ | ||
return ResultBaseFactory.Failure<UnlikeASpixerResponse>("Error liking spixer"); | ||
} | ||
|
||
|
||
|
||
return ResultBaseFactory.Successful( | ||
new UnlikeASpixerResponse(spixer.Id, request.UserId)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Spix.Application/Spixers/Unlike/UnlikeASpixerResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Spix.Application.Spixers.Unlike; | ||
|
||
public class UnlikeASpixerResponse | ||
{ | ||
public Guid SpixerId { get; set; } | ||
public Guid UserId { get; set; } | ||
|
||
public UnlikeASpixerResponse(Guid spixerId, Guid userId) | ||
{ | ||
SpixerId = spixerId; | ||
UserId = userId; | ||
} | ||
} |
Oops, something went wrong.