From 354181e8809ed2abac47fcfc429e1b0033037e33 Mon Sep 17 00:00:00 2001 From: Amadeusz Nowak Date: Fri, 10 May 2024 18:16:56 +0200 Subject: [PATCH 1/9] (#57) add page for creating posts --- .../Areas/Posts/IPostsService.cs | 5 +- .../MiniSpace.Web/Areas/Posts/PostsService.cs | 7 +- .../src/MiniSpace.Web/DTO/PostDto.cs | 3 +- .../Models/Posts/CreatePostModel.cs | 15 +++ .../Pages/Admin/ManageStudents.razor | 6 +- .../MiniSpace.Web/Pages/Events/Event.razor | 44 ++++--- .../EventCreate.razor => EventAdd.razor} | 30 ++--- .../Pages/Events/EventsOrganize.razor | 10 +- .../Pages/Posts/PostCreate.razor | 113 ++++++++++++++++++ 9 files changed, 189 insertions(+), 44 deletions(-) create mode 100644 MiniSpace.Web/src/MiniSpace.Web/Models/Posts/CreatePostModel.cs rename MiniSpace.Web/src/MiniSpace.Web/Pages/Events/{Dialogs/EventCreate.razor => EventAdd.razor} (90%) create mode 100644 MiniSpace.Web/src/MiniSpace.Web/Pages/Posts/PostCreate.razor diff --git a/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/IPostsService.cs b/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/IPostsService.cs index 2cf058fec..b9a6947d5 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/IPostsService.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/IPostsService.cs @@ -2,14 +2,15 @@ using System.Collections.Generic; using System.Threading.Tasks; using MiniSpace.Web.DTO; +using MiniSpace.Web.HttpClients; namespace MiniSpace.Web.Areas.Posts { public interface IPostsService { Task ChangePostStateAsync(Guid postId, string state, DateTime publishDate); - Task CreatePostAsync(Guid postId, Guid eventId, Guid studentId, string textContext, string mediaContext, - string state, DateTime publishedDate); + Task> CreatePostAsync(Guid postId, Guid eventId, Guid organizerId, string textContext, + string mediaContext, string state, DateTime? publishDate); Task DeletePostAsync(Guid postId); Task> GetPostsAsync(Guid eventId); Task UpdatePostAsync(Guid postId, string textContext, string mediaContext); diff --git a/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/PostsService.cs b/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/PostsService.cs index bb7b42e27..3b202e8f5 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/PostsService.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/PostsService.cs @@ -24,11 +24,12 @@ public Task ChangePostStateAsync(Guid postId, string state, DateTime publishDate return _httpClient.PutAsync($"posts/{postId}/state/{state}", new {postId, state, publishDate}); } - public Task CreatePostAsync(Guid postId, Guid eventId, Guid studentId, string textContext, string mediaContext, string state, - DateTime publishedDate) + public Task> CreatePostAsync(Guid postId, Guid eventId, Guid organizerId, string textContent, + string mediaContext, string state, DateTime? publishDate) { _httpClient.SetAccessToken(_identityService.JwtDto.AccessToken); - return _httpClient.PostAsync("posts", new {postId, eventId, studentId, textContext, mediaContext, state, publishedDate}); + return _httpClient.PostAsync("posts", new {postId, eventId, organizerId, textContent, + mediaContext, state, publishDate}); } public Task DeletePostAsync(Guid postId) diff --git a/MiniSpace.Web/src/MiniSpace.Web/DTO/PostDto.cs b/MiniSpace.Web/src/MiniSpace.Web/DTO/PostDto.cs index 2b9cc7596..966461718 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/DTO/PostDto.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/DTO/PostDto.cs @@ -6,11 +6,12 @@ public class PostDto { public Guid Id { get; set; } public Guid EventId { get; set; } - public Guid StudentId { get; set; } + public Guid OrganizerId { get; set; } public string TextContent { get; set; } public string MediaContent { get; set; } public string State { get; set; } public DateTime? PublishDate { get; set; } public DateTime CreatedAt { get; set; } + public DateTime? UpdatedAt { get; set; } } } \ No newline at end of file diff --git a/MiniSpace.Web/src/MiniSpace.Web/Models/Posts/CreatePostModel.cs b/MiniSpace.Web/src/MiniSpace.Web/Models/Posts/CreatePostModel.cs new file mode 100644 index 000000000..d2316ace8 --- /dev/null +++ b/MiniSpace.Web/src/MiniSpace.Web/Models/Posts/CreatePostModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace MiniSpace.Web.Models.Posts +{ + public class CreatePostModel + { + public Guid PostId { get; set; } + public Guid EventId { get; set; } + public Guid OrganizerId { get; set; } + public string TextContent { get; set; } + public string MediaContent { get; set; } + public string State { get; set; } + public DateTime PublishDate { get; set; } + } +} diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Admin/ManageStudents.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Admin/ManageStudents.razor index be86bb6b2..2b404f309 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Admin/ManageStudents.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Admin/ManageStudents.razor @@ -15,7 +15,7 @@ {

Loading...

} -@if (pageInitialized && studentId != Guid.Empty) +@if (pageInitialized && adminId != Guid.Empty) {

To manage rights of a student, open details dialog by clicking "Show" button.

@@ -58,7 +58,7 @@ private int pageSize = 5; IEnumerable pageSizeOptions = new int[] { 5, 10, 20, 40}; - private Guid studentId; + private Guid adminId; private bool pageInitialized = false; private int totalStudents = 0; @@ -68,7 +68,7 @@ { if (IdentityService.IsAuthenticated && IdentityService.GetCurrentUserRole() == "admin") { - studentId = IdentityService.GetCurrentUserId(); + adminId = IdentityService.GetCurrentUserId(); students = await StudentsService.GetStudentsAsync(); totalStudents = students.Count(); diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor index eb259f379..245a5fe7b 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor @@ -1,10 +1,8 @@ @page "/events/{EventId}" -@using MiniSpace.Web.Areas.Students @using MiniSpace.Web.DTO @using MiniSpace.Web.Areas.Events -@using MiniSpace.Web.Areas.Posts -@using MiniSpace.Web.Pages.Admin.Dialogs @using MiniSpace.Web.Pages.Events.Dialogs +@using MiniSpace.Web.Areas.Posts @using Radzen @using AlignItems = Radzen.AlignItems @using DialogOptions = Radzen.DialogOptions @@ -12,7 +10,6 @@ @using Orientation = Radzen.Orientation @inject DialogService DialogService @inject IIdentityService IdentityService -@inject IStudentsService StudentsService @inject IEventsService EventsService @inject IPostsService PostsService @inject NavigationManager NavigationManager @@ -61,7 +58,7 @@ - @if (IdentityService.IsAuthenticated + @if (IdentityService.IsAuthenticated && !IsUserEventOrganizer(ev) && (IdentityService.GetCurrentUserRole() == "user" || IdentityService.GetCurrentUserRole() == "organizer")) { @if (!ev.IsSignedUp) @@ -87,14 +84,19 @@ } } - - @* *@ - @* @if (IdentityService.IsAuthenticated && IdentityService.GetCurrentUserRole() == "organizer" && studentId == ev.Organizer.Id) *@ - @* { *@ - @* *@ - @* *@ - @* } *@ - @* *@ + + @if (IdentityService.IsAuthenticated && IsUserEventOrganizer(ev)) + { + + + + + + + + + } @@ -112,7 +114,7 @@ + Text=@($"{GetAuthorName(post.OrganizerId)}")/> Published at @(post.CreatedAt.ToLocalTime().ToString(dateFormat)) @@ -166,9 +168,19 @@ pageInitialized = true; } - private string GetStudentName(Guid studentId) + private string GetAuthorName(Guid authorId) + { + return authorId == ev.Organizer.Id ? ev.Organizer.Name : "Author unknown"; + } + + private bool IsUserEventOrganizer(EventDto eventDto) { - return studentId == ev.Organizer.Id ? ev.Organizer.Name : "Author unknown"; + if (IdentityService.GetCurrentUserRole() != "organizer") + { + return false; + } + + return ev.Organizer != null && studentId == ev.Organizer.Id; } private async void OnChange(int index) diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Dialogs/EventCreate.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor similarity index 90% rename from MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Dialogs/EventCreate.razor rename to MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor index 07652fe06..bb58a44f4 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Dialogs/EventCreate.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor @@ -1,4 +1,4 @@ -@page "/events/create" +@page "/events/add" @using MiniSpace.Web.Areas.Identity @using MiniSpace.Web.Areas.Events @using MiniSpace.Web.Areas.Http @@ -17,7 +17,7 @@ Shade="Shade.Lighter"> @errorMessage - + @@ -93,7 +93,7 @@ - + @@ -103,11 +103,13 @@ @if (publishInfo == 2) { - - + + + + } @@ -139,7 +141,7 @@ @code { - private UserDto userDto = new(); + private Guid organizerId; private AddEventModel addEventModel = new() { @@ -186,10 +188,10 @@ { if (IdentityService.IsAuthenticated && IdentityService.GetCurrentUserRole() == "organizer") { - userDto = IdentityService.UserDto; - organizations = await OrganizationsService.GetOrganizerOrganizationsAsync(userDto.Id); - - addEventModel.OrganizerId = userDto.Id; + organizerId = IdentityService.GetCurrentUserId(); + organizations = await OrganizationsService.GetOrganizerOrganizationsAsync(organizerId); + + addEventModel.OrganizerId = organizerId; addEventModel.OrganizationId = organizations.First().Id; } } @@ -212,7 +214,7 @@ } else { - NavigationManager.NavigateTo("/events"); + NavigationManager.NavigateTo("/events/organize"); } } } diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventsOrganize.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventsOrganize.razor index 855226cc6..8e5ab86db 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventsOrganize.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventsOrganize.razor @@ -20,8 +20,8 @@
- + @@ -105,7 +105,7 @@ } }; - private Guid studentId; + private Guid organizerId; private bool pageInitialized = false; int totalPages = 0; @@ -116,8 +116,8 @@ { if (IdentityService.IsAuthenticated && IdentityService.GetCurrentUserRole() == "organizer") { - studentId = IdentityService.GetCurrentUserId(); - searchOrganizerEventsModel.OrganizerId = studentId; + organizerId = IdentityService.GetCurrentUserId(); + searchOrganizerEventsModel.OrganizerId = organizerId; var tmp = await EventsService.SearchOrganizerEventsAsync(searchOrganizerEventsModel.OrganizerId, searchOrganizerEventsModel.Name, searchOrganizerEventsModel.State, diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Posts/PostCreate.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Posts/PostCreate.razor new file mode 100644 index 000000000..81198929d --- /dev/null +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Posts/PostCreate.razor @@ -0,0 +1,113 @@ +@page "/events/{EventId}/posts/create" +@using MiniSpace.Web.Areas.Identity +@using MiniSpace.Web.Areas.Http +@using MiniSpace.Web.Areas.Posts +@using MiniSpace.Web.DTO +@using MiniSpace.Web.Models.Posts +@using Radzen +@inject IIdentityService IdentityService +@inject IPostsService PostsService +@inject IErrorMapperService ErrorMapperService +@inject NavigationManager NavigationManager + +

Create new event

+ + @errorMessage + + + + + + + + + + + + + + + + + + + + + + @if (publishInfo == 2) + { + + + + + } + + + + + + + + + + + + + + + +@code { + [Parameter] + public string EventId { get; set; } + + private Guid organizerId; + + private CreatePostModel createPostModel = new() + { + TextContent = "Lorem ipsum!", + MediaContent = "" + }; + private bool showError = false; + private string errorMessage = string.Empty; + private int publishInfo = 1; + + private static bool ValidateDate(DateTime dateTime) + { + return dateTime.Minute % 5 == 0; + } + + protected override async Task OnInitializedAsync() + { + if (IdentityService.IsAuthenticated && IdentityService.GetCurrentUserRole() == "organizer") + { + organizerId = IdentityService.GetCurrentUserId(); + + createPostModel.EventId = new Guid(EventId); + createPostModel.OrganizerId = organizerId; + } + } + + private async Task HandleCreatePost() + { + var response = await PostsService.CreatePostAsync(Guid.Empty, createPostModel.EventId, + createPostModel.OrganizerId, createPostModel.TextContent, createPostModel.MediaContent, + publishInfo == 2 ? "ToBePublished" : "Published", + publishInfo == 2 ? createPostModel.PublishDate : null); + + if (response.ErrorMessage != null) + { + showError = true; + errorMessage = ErrorMapperService.MapError(response.ErrorMessage); + } + else + { + NavigationManager.NavigateTo($"/events/{EventId}"); + } + } +} From dadd7769416ef0d9317768f400294d91f247eb8e Mon Sep 17 00:00:00 2001 From: Amadeusz Nowak Date: Fri, 10 May 2024 19:52:32 +0200 Subject: [PATCH 2/9] (#57) add page for updating events --- .../Areas/Events/EventsService.cs | 10 + .../Areas/Events/IEventsService.cs | 4 + .../HttpClients/CustomHttpClient.cs | 5 +- .../MiniSpace.Web/HttpClients/IHttpClient.cs | 2 +- .../Models/Events/UpdateEventModel.cs | 24 ++ .../MiniSpace.Web/Pages/Events/Event.razor | 3 +- .../MiniSpace.Web/Pages/Events/EventAdd.razor | 4 +- .../Pages/Events/EventUpdate.razor | 226 ++++++++++++++++++ 8 files changed, 272 insertions(+), 6 deletions(-) create mode 100644 MiniSpace.Web/src/MiniSpace.Web/Models/Events/UpdateEventModel.cs create mode 100644 MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventUpdate.razor diff --git a/MiniSpace.Web/src/MiniSpace.Web/Areas/Events/EventsService.cs b/MiniSpace.Web/src/MiniSpace.Web/Areas/Events/EventsService.cs index 1cc0f83e5..86d766afd 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Areas/Events/EventsService.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/Areas/Events/EventsService.cs @@ -43,6 +43,16 @@ public Task> AddEventAsync(Guid eventId, string name, Guid capacity, fee, category, publishDate}); } + public Task> UpdateEventAsync(Guid eventId, string name, Guid organizerId, string startDate, string endDate, + string buildingName, string street, string buildingNumber, string apartmentNumber, string city, string zipCode, + string description, int capacity, decimal fee, string category, string publishDate) + { + _httpClient.SetAccessToken(_identityService.JwtDto.AccessToken); + return _httpClient.PutAsync($"events/{eventId}", new {eventId, name, organizerId, + startDate, endDate, buildingName, street, buildingNumber, apartmentNumber, city, zipCode, description, + capacity, fee, category, publishDate}); + } + public Task SignUpToEventAsync(Guid eventId, Guid studentId) { _httpClient.SetAccessToken(_identityService.JwtDto.AccessToken); diff --git a/MiniSpace.Web/src/MiniSpace.Web/Areas/Events/IEventsService.cs b/MiniSpace.Web/src/MiniSpace.Web/Areas/Events/IEventsService.cs index 125df4438..25cfc42f3 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Areas/Events/IEventsService.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/Areas/Events/IEventsService.cs @@ -16,6 +16,10 @@ Task> AddEventAsync(Guid eventId, string name, Guid organiz string startDate, string endDate, string buildingName, string street, string buildingNumber, string apartmentNumber, string city, string zipCode, string description, int capacity, decimal fee, string category, string publishDate); + Task> UpdateEventAsync(Guid eventId, string name, Guid organizerId, + string startDate, string endDate, string buildingName, string street, string buildingNumber, + string apartmentNumber, string city, string zipCode, string description, int capacity, decimal fee, + string category, string publishDate); Task SignUpToEventAsync(Guid eventId, Guid studentId); Task CancelSignUpToEventAsync(Guid eventId, Guid studentId); Task ShowInterestInEventAsync(Guid eventId, Guid studentId); diff --git a/MiniSpace.Web/src/MiniSpace.Web/HttpClients/CustomHttpClient.cs b/MiniSpace.Web/src/MiniSpace.Web/HttpClients/CustomHttpClient.cs index d84658332..8046db0d5 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/HttpClients/CustomHttpClient.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/HttpClients/CustomHttpClient.cs @@ -63,11 +63,12 @@ public async Task> PostAsync(string uri public Task PutAsync(string uri, T request) => TryExecuteAsync(uri, client => client.PutAsync(uri, GetPayload(request))); - public async Task PutAsync(string uri, TRequest request) + public async Task> PutAsync(string uri, TRequest request) { var (success, content) = await TryExecuteAsync(uri, client => client.PutAsync(uri, GetPayload(request))); - return !success ? default : JsonConvert.DeserializeObject(content, JsonSerializerSettings); + return !success ? new HttpResponse(JsonConvert.DeserializeObject(content, JsonSerializerSettings)) + : new HttpResponse(JsonConvert.DeserializeObject(content, JsonSerializerSettings)); } public Task DeleteAsync(string uri) diff --git a/MiniSpace.Web/src/MiniSpace.Web/HttpClients/IHttpClient.cs b/MiniSpace.Web/src/MiniSpace.Web/HttpClients/IHttpClient.cs index f63bb069b..3eb033935 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/HttpClients/IHttpClient.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/HttpClients/IHttpClient.cs @@ -9,7 +9,7 @@ public interface IHttpClient Task PostAsync(string uri, T request); Task> PostAsync(string uri, TRequest request); Task PutAsync(string uri, T request); - Task PutAsync(string uri, TRequest request); + Task> PutAsync(string uri, TRequest request); Task DeleteAsync(string uri); Task DeleteAsync(string uri, object payload); } diff --git a/MiniSpace.Web/src/MiniSpace.Web/Models/Events/UpdateEventModel.cs b/MiniSpace.Web/src/MiniSpace.Web/Models/Events/UpdateEventModel.cs new file mode 100644 index 000000000..f25a8a737 --- /dev/null +++ b/MiniSpace.Web/src/MiniSpace.Web/Models/Events/UpdateEventModel.cs @@ -0,0 +1,24 @@ +using System; + +namespace MiniSpace.Web.Models.Events +{ + public class UpdateEventModel + { + public Guid EventId { get; set; } + public string Name { get; set; } + public Guid OrganizerId { get; set; } + public DateTime StartDate { get; set; } + public DateTime EndDate { get; set; } + public string BuildingName { get; set; } + public string Street { get; set; } + public string BuildingNumber { get; set; } + public string ApartmentNumber { get; set; } + public string City { get; set; } + public string ZipCode { get; set; } + public string Description { get; set; } + public int Capacity { get; set; } + public decimal Fee { get; set; } + public string Category { get; set; } + public DateTime PublishDate { get; set; } + } +} diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor index 245a5fe7b..ec33b2b60 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor @@ -88,7 +88,8 @@ @if (IdentityService.IsAuthenticated && IsUserEventOrganizer(ev)) { - + diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor index bb58a44f4..d8c4041cd 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor @@ -12,7 +12,7 @@ @inject IErrorMapperService ErrorMapperService @inject NavigationManager NavigationManager -

Create new event

+

Add new event

@errorMessage @@ -60,7 +60,7 @@ - + diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventUpdate.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventUpdate.razor new file mode 100644 index 000000000..85ac9b694 --- /dev/null +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventUpdate.razor @@ -0,0 +1,226 @@ +@page "/events/{EventId}/update" +@using MiniSpace.Web.Areas.Identity +@using MiniSpace.Web.Areas.Events +@using MiniSpace.Web.Areas.Http +@using MiniSpace.Web.DTO +@using MiniSpace.Web.Models.Events +@using Radzen +@inject IIdentityService IdentityService +@inject IEventsService EventsService +@inject IErrorMapperService ErrorMapperService +@inject NavigationManager NavigationManager + +

Update your event

+ +@if (!pageInitialized) +{ +

Loading...

+} + +@if (pageInitialized && organizerId != Guid.Empty) +{ + + @errorMessage + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @if (publishInfo == 2) + { + + + + + } + + + + + + + + + + + + + + + + + + + + + + + + + + +} + +@code { + [Parameter] + public string EventId { get; set; } + + private Guid organizerId; + private EventDto eventDto; + private bool pageInitialized = false; + + private UpdateEventModel updateEventModel = new(); + private bool showError = false; + private string errorMessage = string.Empty; + private int publishInfo = 1; + + private List categories = + [ + "Music", + "Sports", + "Education", + "Science", + "Technology", + "Art", + "Business", + "Health", + "Charity", + "Other" + ]; + + private static bool ValidateDate(DateTime dateTime) + { + return dateTime.Minute % 5 == 0; + } + + protected override async Task OnInitializedAsync() + { + if (IdentityService.IsAuthenticated && IdentityService.GetCurrentUserRole() == "organizer") + { + organizerId = IdentityService.GetCurrentUserId(); + + eventDto = await EventsService.GetEventAsync(new Guid(EventId)); + updateEventModel.EventId = eventDto.Id; + updateEventModel.Name = eventDto.Name; + updateEventModel.OrganizerId = eventDto.Organizer.Id; + updateEventModel.StartDate = eventDto.StartDate; + updateEventModel.EndDate = eventDto.EndDate; + updateEventModel.BuildingName = eventDto.Location.BuildingName; + updateEventModel.Street = eventDto.Location.Street; + updateEventModel.BuildingNumber = eventDto.Location.BuildingNumber; + updateEventModel.ApartmentNumber = eventDto.Location.ApartmentNumber; + updateEventModel.City = eventDto.Location.City; + updateEventModel.ZipCode = eventDto.Location.ZipCode; + updateEventModel.Description = eventDto.Description; + updateEventModel.Capacity = eventDto.Capacity; + updateEventModel.Fee = eventDto.Fee; + updateEventModel.Category = eventDto.Category; + } + + pageInitialized = true; + } + + private async Task HandleUpdateEvent() + { + var response = await EventsService.UpdateEventAsync(updateEventModel.EventId, + updateEventModel.Name, updateEventModel.OrganizerId, + updateEventModel.StartDate.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"), + updateEventModel.EndDate.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"), + updateEventModel.BuildingName, updateEventModel.Street, updateEventModel.BuildingNumber, + updateEventModel.ApartmentNumber, updateEventModel.City, updateEventModel.ZipCode, + updateEventModel.Description, updateEventModel.Capacity, updateEventModel.Fee, updateEventModel.Category, + publishInfo == 2 ? updateEventModel.PublishDate.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") : string.Empty); + + if (response.ErrorMessage != null) + { + showError = true; + errorMessage = ErrorMapperService.MapError(response.ErrorMessage); + } + else + { + NavigationManager.NavigateTo($"/events/{EventId}"); + } + } +} From 9bf050e840253ab0492426fd2a2329fb0839dc77 Mon Sep 17 00:00:00 2001 From: Amadeusz Nowak Date: Sun, 12 May 2024 17:02:56 +0200 Subject: [PATCH 3/9] (#57) update page for creating events to initalize properly --- .../MiniSpace.Web/Pages/Events/EventAdd.razor | 266 +++++++++--------- 1 file changed, 139 insertions(+), 127 deletions(-) diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor index d8c4041cd..da81391ab 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/EventAdd.razor @@ -13,136 +13,146 @@ @inject NavigationManager NavigationManager

Add new event

- - @errorMessage - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @if (publishInfo == 2) - { - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - + +@if (!pageInitialized) +{ +

Loading...

+} + +@if (pageInitialized && organizerId != Guid.Empty) +{ + + @errorMessage + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @if (publishInfo == 2) + { + + + + + } + + + + + + + + + + + + + + + + + + + + + + + + + + +} @code { private Guid organizerId; - + private bool pageInitialized = false; + private AddEventModel addEventModel = new() { Name = "One of first events!", @@ -194,6 +204,8 @@ addEventModel.OrganizerId = organizerId; addEventModel.OrganizationId = organizations.First().Id; } + + pageInitialized = true; } private async Task HandleCreateEvent() From d4a8c816b09d5d651d90a4558d49751c58ff5ae1 Mon Sep 17 00:00:00 2001 From: Amadeusz Nowak Date: Sun, 12 May 2024 17:07:53 +0200 Subject: [PATCH 4/9] (#57) update page for showing an event to initalize properly --- .../MiniSpace.Web/Pages/Events/Event.razor | 240 +++++++++--------- 1 file changed, 124 insertions(+), 116 deletions(-) diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor index ec33b2b60..34a1c15c8 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor @@ -14,136 +14,144 @@ @inject IPostsService PostsService @inject NavigationManager NavigationManager -

@ev.Name

+@if (!pageInitialized) +{ +

Loading...

+} - - - - - Description - @(ev.Description) - @if (ev.Organizer != null) - { - Organization - @(ev.Organizer.OrganizationName) - Organizer - @(ev.Organizer.Name) - } +@if (pageInitialized) +{ +

@ev.Name

+ + + + + + Description + @(ev.Description) + @if (ev.Organizer != null) + { + Organization + @(ev.Organizer.OrganizationName) + Organizer + @(ev.Organizer.Name) + } + - - - - - Start date - @(ev.StartDate.ToLocalTime().ToString(dateFormat)) - Category - @(ev.Category) - - - End date - @(ev.EndDate.ToLocalTime().ToString(dateFormat)) - Fee - @(ev.Fee) - - + + + + Start date + @(ev.StartDate.ToLocalTime().ToString(dateFormat)) + Category + @(ev.Category) + + + End date + @(ev.EndDate.ToLocalTime().ToString(dateFormat)) + Fee + @(ev.Fee) + + + -
- - - - - - - - @if (IdentityService.IsAuthenticated && !IsUserEventOrganizer(ev) - && (IdentityService.GetCurrentUserRole() == "user" || IdentityService.GetCurrentUserRole() == "organizer")) - { - @if (!ev.IsSignedUp) - { - - } - else - { - - } - - @if (!ev.IsInterested) - { - - } - else - { - - } - } - - - @if (IdentityService.IsAuthenticated && IsUserEventOrganizer(ev)) - { + - - + + - + - - - } - - - - - - @if (pageInitialized && !posts.Any()) + @if (IdentityService.IsAuthenticated && !IsUserEventOrganizer(ev) + && (IdentityService.GetCurrentUserRole() == "user" || IdentityService.GetCurrentUserRole() == "organizer")) { -

No posts have been added by organizers yet.

+ @if (!ev.IsSignedUp) + { + + } + else + { + + } + + @if (!ev.IsInterested) + { + + } + else + { + + } } - - + +
- - + + - - -
-
-
+ + + + +
+} @code { [Parameter] From d2e5512d2a6d7b3a7f37fc4ce824cd9b2933aedb Mon Sep 17 00:00:00 2001 From: Amadeusz Nowak Date: Sun, 12 May 2024 17:53:01 +0200 Subject: [PATCH 5/9] (#57) add basic version of page for showing a post --- .../Areas/Posts/IPostsService.cs | 1 + .../MiniSpace.Web/Areas/Posts/PostsService.cs | 5 + .../MiniSpace.Web/Pages/Events/Event.razor | 2 +- .../src/MiniSpace.Web/Pages/Posts/Post.razor | 109 ++++++++++++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 MiniSpace.Web/src/MiniSpace.Web/Pages/Posts/Post.razor diff --git a/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/IPostsService.cs b/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/IPostsService.cs index b9a6947d5..b24e531d6 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/IPostsService.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/IPostsService.cs @@ -8,6 +8,7 @@ namespace MiniSpace.Web.Areas.Posts { public interface IPostsService { + Task GetPostAsync(Guid postId); Task ChangePostStateAsync(Guid postId, string state, DateTime publishDate); Task> CreatePostAsync(Guid postId, Guid eventId, Guid organizerId, string textContext, string mediaContext, string state, DateTime? publishDate); diff --git a/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/PostsService.cs b/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/PostsService.cs index 3b202e8f5..efd1862f0 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/PostsService.cs +++ b/MiniSpace.Web/src/MiniSpace.Web/Areas/Posts/PostsService.cs @@ -17,6 +17,11 @@ public PostsService(IHttpClient httpClient, IIdentityService identityService) _httpClient = httpClient; _identityService = identityService; } + + public Task GetPostAsync(Guid postId) + { + return _httpClient.GetAsync($"posts/{postId}"); + } public Task ChangePostStateAsync(Guid postId, string state, DateTime publishDate) { diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor index 34a1c15c8..622177200 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor @@ -189,7 +189,7 @@ return false; } - return ev.Organizer != null && studentId == ev.Organizer.Id; + return eventDto.Organizer != null && studentId == eventDto.Organizer.Id; } private async void OnChange(int index) diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Posts/Post.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Posts/Post.razor new file mode 100644 index 000000000..85849cbc3 --- /dev/null +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Posts/Post.razor @@ -0,0 +1,109 @@ +@page "/posts/{PostId}" +@using MiniSpace.Web.DTO +@using MiniSpace.Web.Areas.Events +@using MiniSpace.Web.Pages.Events.Dialogs +@using MiniSpace.Web.Areas.Posts +@using Radzen +@using AlignItems = Radzen.AlignItems +@using DialogOptions = Radzen.DialogOptions +@using DialogService = Radzen.DialogService +@using Orientation = Radzen.Orientation +@inject DialogService DialogService +@inject IIdentityService IdentityService +@inject IPostsService PostsService +@inject NavigationManager NavigationManager + +@if (!pageInitialized) +{ +

Loading...

+} + +@if (pageInitialized) +{ +

@AuthorName

+ + + + + + + + + Published at @(post.CreatedAt.ToLocalTime().ToString(dateFormat)) + + + + +
+ + + + + @(post.TextContent) + + + +
+ + + + + + + @if (IdentityService.IsAuthenticated && IsUserPostCreator(post)) + { + + + + + } + + + + + + + + + + + +
+} + +@code { + [Parameter] + public string PostId { get; set; } + [Parameter] + public string AuthorName { get; set; } + + private const string dateFormat = "dd/MM/yyyy HH:mm"; + + private Guid studentId; + private PostDto post = new(); + private bool pageInitialized = false; + + protected override async Task OnInitializedAsync() + { + if (IdentityService.IsAuthenticated) + { + studentId = IdentityService.GetCurrentUserId(); + } + + pageInitialized = true; + } + + private bool IsUserPostCreator(PostDto postDto) + { + if (IdentityService.GetCurrentUserRole() != "organizer") + { + return false; + } + + return studentId == postDto.OrganizerId; + } +} From e3133a2965838b98c7d099903e100339cf29c82f Mon Sep 17 00:00:00 2001 From: Amadeusz Nowak Date: Sun, 12 May 2024 18:09:36 +0200 Subject: [PATCH 6/9] (#57) add endpoint in Posts for getting a post by id --- .../MiniSpace.APIGateway/ntrada.docker.yml | 5 ++++ .../src/MiniSpace.APIGateway/ntrada.yml | 5 ++++ .../MiniSpace.Services.Posts.Api/Program.cs | 1 + .../Queries/GetPost.cs | 10 ++++++++ .../Mongo/Queries/Handlers/GetPostHandler.cs | 25 +++++++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs create mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs diff --git a/MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.docker.yml b/MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.docker.yml index b42172de4..0a6ecc631 100644 --- a/MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.docker.yml +++ b/MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.docker.yml @@ -503,6 +503,11 @@ modules: - state:{state} auth: true + - upstream: /{postId} + method: GET + use: downstream + downstream: posts-service/posts/{postId} + - upstream: / method: GET use: downstream diff --git a/MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.yml b/MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.yml index 758b51aeb..55eadd71d 100644 --- a/MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.yml +++ b/MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.yml @@ -559,6 +559,11 @@ modules: - state:{state} auth: true + - upstream: /{postId} + method: GET + use: downstream + downstream: posts-service/posts/{postId} + - upstream: / method: GET use: downstream diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs index b644c5289..bdbec8ae4 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs @@ -31,6 +31,7 @@ public static async Task Main(string[] args) .UseInfrastructure() .UseDispatcherEndpoints(endpoints => endpoints .Get("", ctx => ctx.Response.WriteAsync(ctx.RequestServices.GetService().Name)) + .Get("posts/{postId}") .Get>("posts") .Get>("posts/organizer/{organizerId}") .Put("posts/{postId}") diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs new file mode 100644 index 000000000..3524d49c6 --- /dev/null +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs @@ -0,0 +1,10 @@ +using Convey.CQRS.Queries; +using MiniSpace.Services.Posts.Application.Dto; + +namespace MiniSpace.Services.Posts.Application.Queries +{ + public class GetPost : IQuery + { + public Guid PostId { get; set; } + } +} diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs new file mode 100644 index 000000000..4a6447b53 --- /dev/null +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs @@ -0,0 +1,25 @@ +using Convey.CQRS.Queries; +using Convey.Persistence.MongoDB; +using MiniSpace.Services.Posts.Application.Dto; +using MiniSpace.Services.Posts.Application.Queries; +using MiniSpace.Services.Posts.Infrastructure.Mongo.Documents; + +namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Queries.Handlers +{ + public class GetPostHandler : IQueryHandler + { + private readonly IMongoRepository _repository; + + public GetPostHandler(IMongoRepository repository) + { + _repository = repository; + } + + public async Task HandleAsync(GetPost query, CancellationToken cancellationToken) + { + var post = await _repository.GetAsync(query.PostId); + + return post?.AsDto(); + } + } +} From 43ba1190eb31528c030bf1c7d7d90b967ac68aa3 Mon Sep 17 00:00:00 2001 From: Amadeusz Nowak Date: Sun, 12 May 2024 18:25:44 +0200 Subject: [PATCH 7/9] (#57) update page for showing a post --- .../MiniSpace.Web/Pages/Events/Event.razor | 22 +++++++++++---- .../src/MiniSpace.Web/Pages/Posts/Post.razor | 27 ++++++++++++------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor index 622177200..cad036f80 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Event.razor @@ -107,7 +107,7 @@ } - + @if (pageInitialized && !posts.Any()) @@ -117,15 +117,24 @@