Skip to content

Commit

Permalink
Merge pull request #166 from SaintAngeLs/posts_service
Browse files Browse the repository at this point in the history
Posts service update
  • Loading branch information
eggwhat authored May 21, 2024
2 parents ef293a9 + f4622ed commit 66a7be1
Show file tree
Hide file tree
Showing 25 changed files with 382 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ modules:
use: downstream
downstream: posts-service/posts
auth: true

- upstream: /search
method: POST
use: downstream
downstream: posts-service/posts/search
auth: true

- upstream: /{postId}
method: PUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,12 @@ modules:
downstream: posts-service/posts
auth: true

- upstream: /search
method: POST
use: downstream
downstream: posts-service/posts/search
auth: true

- upstream: /{postId}
method: PUT
use: downstream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,12 @@ modules:
downstream: posts-service/posts
auth: true

- upstream: /search
method: POST
use: downstream
downstream: posts-service/posts/search
auth: true

- upstream: /{postId}
method: PUT
use: downstream
Expand Down
6 changes: 6 additions & 0 deletions MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.yml
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ modules:
downstream: posts-service/posts
auth: true

- upstream: /search
method: POST
use: downstream
downstream: posts-service/posts/search
auth: true

- upstream: /{postId}
method: PUT
use: downstream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using MiniSpace.Services.Posts.Application.Commands;
using MiniSpace.Services.Posts.Application.Dto;
using MiniSpace.Services.Posts.Application.Queries;
using MiniSpace.Services.Posts.Application.Services;
using MiniSpace.Services.Posts.Infrastructure;

namespace MiniSpace.Services.Posts.Api
Expand All @@ -29,6 +30,12 @@ public static async Task Main(string[] args)
.Build())
.Configure(app => app
.UseInfrastructure()
.UseEndpoints(endpoints => endpoints
.Post<SearchPosts>("posts/search", async (cmd, ctx) =>
{
var pagedResult = await ctx.RequestServices.GetService<IPostsService>().BrowsePostsAsync(cmd);
await ctx.Response.WriteJsonAsync(pagedResult);
}))
.UseDispatcherEndpoints(endpoints => endpoints
.Get("", ctx => ctx.Response.WriteAsync(ctx.RequestServices.GetService<AppOptions>().Name))
.Get<GetPost, PostDto>("posts/{postId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"httpClient": {
"type": "fabio",
"retries": 3,
"services": {}
"services": {
"students": "students-service"
}
},
"jwt": {
"certificate": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
"service": "posts-service"
},
"httpClient": {
"type": "fabio",
"type": "direct",
"retries": 3,
"services": {},
"services": {
"students": "http://localhost:5007"
},
"requestMasking": {
"enabled": true,
"maskTemplate": "*****"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Convey.CQRS.Commands;
using MiniSpace.Services.Posts.Application.Dto;

namespace MiniSpace.Services.Posts.Application.Commands
{
public class SearchPosts : ICommand
{
public Guid StudentId { get; set; }
public PageableDto Pageable { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MiniSpace.Services.Posts.Application.Dto
{
public class PageableDto
{
public int Page { get; set; }
public int Size { get; set; }
public SortDto Sort { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using MiniSpace.Services.Posts.Core.Entities;

namespace MiniSpace.Services.Posts.Application.Dto
{
public class PostDto
Expand All @@ -11,5 +13,22 @@ public class PostDto
public DateTime? PublishDate { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }

public PostDto()
{
}

public PostDto(Post post)
{
Id = post.Id;
EventId = post.EventId;
OrganizerId = post.OrganizerId;
TextContent = post.TextContent;
MediaFiles = post.MediaFiles;
State = post.State.ToString();
PublishDate = post.PublishDate;
CreatedAt = post.CreatedAt;
UpdatedAt = post.UpdatedAt;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MiniSpace.Services.Posts.Application.Dto
{
public class SortDto
{
public IEnumerable<string> SortBy { get; set; }
public string Direction { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MiniSpace.Services.Posts.Application.Dto
{
public class StudentEventsDto
{
public Guid StudentId { get; set; }
public IEnumerable<Guid> InterestedInEvents { get; set; }
public IEnumerable<Guid> SignedUpEvents { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MiniSpace.Services.Posts.Application.Exceptions
{
public class UnauthorizedPostSearchException : AppException
{
public override string Code { get; } = "unauthorized_post_search";
public Guid StudentId { get; }
public Guid IdentityId { get; }

public UnauthorizedPostSearchException(Guid studentId, Guid identityId) : base("Unauthorized post search.")
{
StudentId = studentId;
IdentityId = identityId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MiniSpace.Services.Posts.Application.Dto;

namespace MiniSpace.Services.Posts.Application.Services.Clients
{
public interface IStudentsServiceClient
{
Task<StudentEventsDto> GetAsync(Guid id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using MiniSpace.Services.Posts.Application.Commands;
using MiniSpace.Services.Posts.Application.Dto;
using MiniSpace.Services.Posts.Core.Wrappers;

namespace MiniSpace.Services.Posts.Application.Services
{
public interface IPostsService
{
Task<PagedResponse<IEnumerable<PostDto>>> BrowsePostsAsync(SearchPosts command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void SetToBePublished(DateTime publishDate, DateTime now)
public void SetPublished(DateTime now)
{
State = State.Published;
PublishDate = null;
PublishDate = now;
UpdatedAt = now;
}

Expand Down Expand Up @@ -72,7 +72,8 @@ public static Post Create(AggregateId id, Guid eventId, Guid studentId, string t
{
CheckTextContent(id, textContent);

return new Post(id, eventId, studentId, textContent, mediaFiles, createdAt, state, publishDate);
return new Post(id, eventId, studentId, textContent, mediaFiles, createdAt, state,
publishDate ?? createdAt);
}

public void Update(string textContent, IEnumerable<Guid> mediaFiles, DateTime now)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MiniSpace.Services.Posts.Core.Exceptions
{
public class InvalidStudentServiceClientResponseException : DomainException
{
public override string Code { get; } = "invalid_student_service_client_response";
public Guid StudentId { get; }

public InvalidStudentServiceClientResponseException(Guid studentId)
: base($"Invalid student service client response for student with ID: '{studentId}'.")
{
StudentId = studentId;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniSpace.Services.Posts.Core.Entities;
using MiniSpace.Services.Posts.Core.Wrappers;

namespace MiniSpace.Services.Posts.Core.Repositories
{
Expand All @@ -11,5 +12,7 @@ public interface IPostRepository
Task UpdateAsync(Post post);
Task DeleteAsync(Guid id);
Task<bool> ExistsAsync(Guid id);
Task<(IEnumerable<Post> posts, int pageNumber,int pageSize, int totalPages, int totalElements)> BrowseCommentsAsync(int pageNumber, int pageSize,
IEnumerable<Guid> eventsIds, IEnumerable<string> sortBy, string direction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace MiniSpace.Services.Posts.Core.Wrappers
{
public class PagedResponse<T> : Response<T>
{
public int TotalPages { get; }
public int TotalElements { get; }
public int Size { get; }
public int Number { get; }
public bool First { get; }
public bool Last { get; }
public bool Empty { get; }

public PagedResponse(T content, int pageNumber, int pageSize, int totalPages, int totalElements)
{
Content = content;
TotalPages = totalPages;
TotalElements = totalElements;
Size = pageSize;
Number = pageNumber;
First = pageNumber == 0;
Last = pageNumber == totalPages - 1;
Empty = totalElements == 0;
Succeeded = true;
Errors = null;
Message = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MiniSpace.Services.Posts.Core.Wrappers
{
public class Response<T>
{
public T Content { get; set; }
public bool Succeeded { get; set; }
public string[] Errors { get; set; }
public string Message { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using MiniSpace.Services.Events.Infrastructure.Services.Clients;
using Newtonsoft.Json;
using MiniSpace.Services.Posts.Application;
using MiniSpace.Services.Posts.Application.Commands;
using MiniSpace.Services.Posts.Application.Events.External;
using MiniSpace.Services.Posts.Application.Services;
using MiniSpace.Services.Posts.Application.Services.Clients;
using MiniSpace.Services.Posts.Core.Repositories;
using MiniSpace.Services.Posts.Infrastructure.Contexts;
using MiniSpace.Services.Posts.Infrastructure.Decorators;
Expand All @@ -52,6 +54,8 @@ public static IConveyBuilder AddInfrastructure(this IConveyBuilder builder)
builder.Services.AddSingleton<IEventMapper, EventMapper>();
builder.Services.AddTransient<IMessageBroker, MessageBroker>();
builder.Services.AddTransient<IAppContextFactory, AppContextFactory>();
builder.Services.AddTransient<IPostsService, PostsService>();
builder.Services.AddTransient<IStudentsServiceClient, StudentsServiceClient>();
builder.Services.AddTransient(ctx => ctx.GetRequiredService<IAppContextFactory>().Create());
builder.Services.TryDecorate(typeof(ICommandHandler<>), typeof(OutboxCommandHandlerDecorator<>));
builder.Services.TryDecorate(typeof(IEventHandler<>), typeof(OutboxEventHandlerDecorator<>));
Expand Down
Loading

0 comments on commit 66a7be1

Please sign in to comment.