Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: created posts #12

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Living.Application/Living.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="8.2.5" />
<PackageReference Include="MediatR" Version="12.4.0" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.163">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -17,7 +18,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Living.Domain.Features.Users;
using Living.Domain.Features.Users.Constants;
using Living.Domain.Features.Users.Events;
using Living.Shared.Extensions;
using MassTransit;
using Microsoft.AspNetCore.Identity;

namespace Living.Application.UseCases.Users.Register;
public class RegisterUserHandler(UserManager<User> userManager, IUnitOfWork unitOfWork)
public class RegisterUserHandler(UserManager<User> userManager, IUnitOfWork unitOfWork, IPublishEndpoint publish, IMessageScheduler messageScheduler)
: Handler(unitOfWork), IRequestHandler<RegisterUserCommand, BaseResponse<Guid>>
{
public async Task<BaseResponse<Guid>> Handle(RegisterUserCommand request, CancellationToken cancellationToken)
Expand All @@ -22,6 +24,9 @@ public async Task<BaseResponse<Guid>> Handle(RegisterUserCommand request, Cancel
if (!result.Succeeded)
return new(result.Errors);

await publish.Publish(new UserCreatedEvent(user.Id), cancellationToken);
await messageScheduler.SchedulePublish(DateTime.Now.AddSeconds(10), new UserCreatedEvent(user.Id));

return new(user.Id);
}
}
}
11 changes: 11 additions & 0 deletions Living.Domain/Base/Paginated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Living.Domain.Base;
public class Paginated<T>(IEnumerable<T> items, int pageIndex, int pageSize, long totalCount)
{
public IEnumerable<T> Items { get; } = items;
public int PageIndex { get; } = pageIndex;
public int PageSize { get; } = pageSize;
public long TotalCount { get; } = totalCount;
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
}
19 changes: 0 additions & 19 deletions Living.Domain/Features/Groups/Group.cs

This file was deleted.

15 changes: 0 additions & 15 deletions Living.Domain/Features/Groups/GroupUser.cs

This file was deleted.

1 change: 0 additions & 1 deletion Living.Domain/Features/Posts/Enums/PostAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ public enum PostAccess
{
Public,
Followers,
Group,
Private
}
2 changes: 2 additions & 0 deletions Living.Domain/Features/Posts/Events/PostScheduledEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
namespace Living.Domain.Features.Posts.Events;
public record PostScheduledEvent(Guid PostId);
5 changes: 1 addition & 4 deletions Living.Domain/Features/Posts/Post.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Living.Domain.Features.Groups;
using Living.Domain.Features.Posts.Enums;
using Living.Domain.Features.Posts.Enums;

namespace Living.Domain.Features.Posts;
public class Post : IEntity, ITimestamps, ISoftDelete
Expand All @@ -9,7 +8,6 @@ public class Post : IEntity, ITimestamps, ISoftDelete
public required Guid AuthorId { get; set; }
public required PostAccess Access { get; set; }
public required PostType Type { get; set; }
public Guid? GroupId { get; set; }
public Guid? PostParentId { get; set; }
public Guid? PostChildId { get; set; }

Expand All @@ -24,7 +22,6 @@ public class Post : IEntity, ITimestamps, ISoftDelete
public Post? PostChild { get; init; }
public List<Post> PostsChildrens { get; init; } = [];
public User Author { get; init; }
public Group? Group { get; init; }
public List<PostLike> PostLikes { get; init; } = [];

public void AddLike(Guid userId)
Expand Down
2 changes: 2 additions & 0 deletions Living.Domain/Features/Users/Events/UserCreatedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
namespace Living.Domain.Features.Users.Events;
public record UserCreatedEvent(Guid UserId);
6 changes: 1 addition & 5 deletions Living.Domain/Features/Users/User.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Living.Domain.Features.Groups;
using Living.Domain.Features.Posts;
using Living.Domain.Features.Posts;
using Living.Domain.Features.Roles;
using Living.Domain.Features.Users.Constants;
using Microsoft.AspNetCore.Identity;
Expand All @@ -26,9 +25,6 @@ public class User : IdentityUser<Guid>, IEntity, ITimestamps, IValidit
public List<UserFollow> UsersFollowers { get; set; } = [];
public List<UserFollow> UsersFollowing { get; set; } = [];

public List<GroupUser> GroupsUser { get; set; } = [];
public List<Group> GroupsOwned { get; set; } = [];

public List<UserRole> UserRoles { get; set; } = [];
public List<UserClaim> UserClaims { get; set; } = [];
public List<UserLogin> UserLogins { get; set; } = [];
Expand Down
5 changes: 0 additions & 5 deletions Living.Domain/Mapper/IUpdateEntity.cs

This file was deleted.

37 changes: 0 additions & 37 deletions Living.Infraestructure/Configuration/GroupConfiguration.cs

This file was deleted.

7 changes: 0 additions & 7 deletions Living.Infraestructure/Configuration/PostConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public void Configure(EntityTypeBuilder<Post> builder)
builder.Property(x => x.PostChildId)
.IsRequired(false);

builder.Property(x => x.GroupId)
.IsRequired(false);

builder.HasOne(x => x.Author)
.WithMany(x => x.Posts)
.HasForeignKey(x => x.AuthorId);
Expand All @@ -40,10 +37,6 @@ public void Configure(EntityTypeBuilder<Post> builder)
.WithOne(x => x.Post)
.HasForeignKey(x => x.PostId);

builder.HasOne(x => x.Group)
.WithMany(x => x.Posts)
.HasForeignKey(x => x.GroupId);

builder.HasOne(x => x.PostChild)
.WithMany(x => x.PostsParent)
.HasForeignKey(x => x.PostChildId);
Expand Down
8 changes: 0 additions & 8 deletions Living.Infraestructure/Configuration/UserConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ public void Configure(EntityTypeBuilder<User> builder)
.WithOne(e => e.Following)
.HasForeignKey(e => e.FollowingId);

builder.HasMany(e => e.GroupsUser)
.WithOne(e => e.User)
.HasForeignKey(e => e.UserId);

builder.HasMany(e => e.GroupsOwned)
.WithOne(e => e.Owner)
.HasForeignKey(e => e.OwnerId);

builder.HasMany(e => e.UserClaims)
.WithOne(p => p.User)
.HasForeignKey(e => e.UserId);
Expand Down
5 changes: 1 addition & 4 deletions Living.Infraestructure/Context/DatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Living.Domain.Features.Groups;
using Living.Domain.Features.Posts;
using Living.Domain.Features.Posts;
using Living.Domain.Features.Roles;
using Living.Domain.Features.Users;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
Expand All @@ -13,8 +12,6 @@ public class DatabaseContext(DbContextOptions options)
public DbSet<UserFollow> UserFollows => Set<UserFollow>();
public DbSet<Post> Posts => Set<Post>();
public DbSet<PostLike> PostLikes => Set<PostLike>();
public DbSet<Group> Groups => Set<Group>();
public DbSet<GroupUser> GroupUsers => Set<GroupUser>();

protected override void OnModelCreating(ModelBuilder builder)
{
Expand Down
83 changes: 83 additions & 0 deletions Living.Infraestructure/Extensions/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Living.Domain.Features.Posts.Interface;
using Living.Domain.Features.Users.Interfaces;
using Living.Domain.Services;
using Living.Infraestructure.Context;
using Living.Infraestructure.Context.Interceptors;
using Living.Infraestructure.Repositories;
using Living.Infraestructure.Services;
using Living.Infraestructure.Settings;
using Living.Infraestructure.UnitOfWorks;
using Living.Shared.Extensions;
using Living.Shared.Handlers;
using MassTransit;
using Microsoft.Extensions.DependencyInjection;

namespace Living.Infraestructure.Extensions;

public static class DependencyInjectionExtensions
{
public static void AddDatabase(this IServiceCollection services)
{
var connectionStrings = services.GetOptions<ConnectionStrings>();

services.AddDbContext<DatabaseContext>(options =>
{
options.AddInterceptors(new SavingChangesInterceptor());
options.UseNpgsql(connectionStrings.PostgresConnection);
});
}

public static void AddMessaging(this IServiceCollection services)
{
services.AddMessaging((e) => { });
}

public static void AddMessaging(this IServiceCollection services, Action<IBusRegistrationConfigurator> action)
{
var connectionStrings = services.GetOptions<ConnectionStrings>();

services.AddMassTransit(x =>
{
action(x);

x.ConfigureTeste(connectionStrings.RabbitMqConnection);
});
}

public static void ConfigureTeste(this IBusRegistrationConfigurator busRegistrationConfigurator, string connectionString)
{
busRegistrationConfigurator.AddDelayedMessageScheduler();



busRegistrationConfigurator.UsingRabbitMq((context, cfg) =>
{
cfg.Host(new Uri(connectionString), "Living");

cfg.UseDelayedMessageScheduler();

cfg.ConfigureEndpoints(context);

cfg.UseMessageRetry(retry => { retry.Interval(3, TimeSpan.FromSeconds(5)); });
});
}

public static void AddApplication(this IServiceCollection services)
{
services.AddScoped<IUnitOfWork, UnitOfWork>();

services.AddRepositories();
services.AddServices();
}

private static void AddRepositories(this IServiceCollection services)
{
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IPostRepository, PostRepository>();
}

private static void AddServices(this IServiceCollection services)
{
services.AddScoped<ITokenService, TokenService>();
}
}
6 changes: 4 additions & 2 deletions Living.Infraestructure/Living.Infraestructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="FluentMigrator" Version="5.2.0" />
<PackageReference Include="FluentMigrator.Runner.Postgres" Version="5.2.0" />
<PackageReference Include="MassTransit" Version="8.2.5" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.2.5" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.163">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -18,13 +20,13 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.0.1" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.0.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions Living.Infraestructure/Settings/ConnectionStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ public class ConnectionStrings
{
[Required]
public string PostgresConnection { get; set; }

[Required]
public string RabbitMqConnection { get; set; }
}
27 changes: 27 additions & 0 deletions Living.Shared/Extensions/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using IServiceCollection = Microsoft.Extensions.DependencyInjection.IServiceCollection;
using IServiceProvider = Microsoft.Extensions.DependencyInjection.IServiceCollection;

namespace Living.Shared.Extensions;
public static class IServiceCollectionExtensions
{
public static TOptions GetOptions<TOptions>(this IServiceProvider services)
where TOptions : class
{
using var serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetRequiredService<IOptions<TOptions>>().Value;
}

public static IServiceCollection AddOptionsConfiguration<T>(this IServiceCollection services, IConfiguration configuration)
where T : class
{
services.AddOptions<T>()
.Bind(configuration.GetSection(typeof(T).Name))
.ValidateDataAnnotations()
.ValidateOnStart();

return services;
}
}
5 changes: 5 additions & 0 deletions Living.Shared/Living.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Loading
Loading