From d440ac90e4909375dbccab609a3ae0bc94430b51 Mon Sep 17 00:00:00 2001 From: berkansasmaz Date: Wed, 15 Dec 2021 14:26:56 +0300 Subject: [PATCH] feat: add ability to add speakers with Session --- .../Events/AddSessionDto.cs | 8 ++ .../Events/EventAppService.cs | 32 +++++- .../Users/UserAppService.cs | 2 +- src/EventHub.Domain/Events/Event.cs | 5 +- src/EventHub.Domain/Events/Session.cs | 29 ++++- src/EventHub.Domain/Events/Track.cs | 5 +- .../Controllers/Users/UserController.cs | 1 + .../CreateEventArea/_addSessionSection.cshtml | 30 ++--- .../CreateEventArea/create-event-area.css | 3 + .../CreateEventArea/create-event-area.js | 107 +++++++++++------- src/EventHub.Web/Pages/Events/New.cshtml | 5 + 11 files changed, 162 insertions(+), 65 deletions(-) diff --git a/src/EventHub.Application.Contracts/Events/AddSessionDto.cs b/src/EventHub.Application.Contracts/Events/AddSessionDto.cs index d91e20d..c1609e9 100644 --- a/src/EventHub.Application.Contracts/Events/AddSessionDto.cs +++ b/src/EventHub.Application.Contracts/Events/AddSessionDto.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace EventHub.Events; @@ -24,4 +25,11 @@ public class AddSessionDto public string Description { get; set; } public string Language { get; set; } + + public List SpeakerUserNames { get; set; } + + public AddSessionDto() + { + SpeakerUserNames = new List(); + } } diff --git a/src/EventHub.Application/Events/EventAppService.cs b/src/EventHub.Application/Events/EventAppService.cs index 01f669d..cdb36f0 100644 --- a/src/EventHub.Application/Events/EventAppService.cs +++ b/src/EventHub.Application/Events/EventAppService.cs @@ -7,6 +7,7 @@ using EventHub.Events.Registrations; using EventHub.Organizations; using EventHub.Users; using Microsoft.AspNetCore.Authorization; +using Volo.Abp; using Volo.Abp.Application.Dtos; using Volo.Abp.Authorization; using Volo.Abp.BlobStoring; @@ -184,6 +185,14 @@ namespace EventHub.Events dto.OrganizationDisplayName = organization.DisplayName; var user = await _userRepository.GetAsync(organization.OwnerUserId); + if (@event.IsDraft) + { + if (user.Id != CurrentUser.GetId()) + { + throw new AbpAuthorizationException(); + } + } + dto.OwnerUserName = user.UserName; dto.OwnerEmail = user.Email; @@ -302,6 +311,7 @@ namespace EventHub.Events public async Task AddSessionAsync(Guid id, AddSessionDto input) { var @event = await _eventRepository.GetAsync(id); + @event.AddSession( input.TrackId, GuidGenerator.Create(), @@ -309,8 +319,10 @@ namespace EventHub.Events input.StartTime, input.EndTime, input.Description, - input.Language + input.Language, + await GetUserIdsByUserNamesAsync(input.SpeakerUserNames) ); + await _eventRepository.UpdateAsync(@event); } @@ -334,6 +346,24 @@ namespace EventHub.Events await _eventBlobContainer.SaveAsync(blobName, streamContent.GetStream(), overrideExisting: true); } + + private async Task> GetUserIdsByUserNamesAsync(List userNames) + { + var speakerUserIDs = new List(); + foreach (var userName in userNames.Where(userName => !userName.IsNullOrWhiteSpace())) + { + var user = await _userRepository.FindAsync(x => x.UserName == userName.Trim()); + if (user is null) + { + throw new BusinessException(EventHubErrorCodes.UserNotFound) + .WithData("UserName", userName); + } + + speakerUserIDs.Add(user.Id); + } + + return speakerUserIDs; + } private async Task CheckOwnerControlAsync(Event @event) { diff --git a/src/EventHub.Application/Users/UserAppService.cs b/src/EventHub.Application/Users/UserAppService.cs index e7b095b..4cf6a52 100644 --- a/src/EventHub.Application/Users/UserAppService.cs +++ b/src/EventHub.Application/Users/UserAppService.cs @@ -29,7 +29,7 @@ namespace EventHub.Users var userQueryable = await _userRepository.GetQueryableAsync(); var query = userQueryable - .Where(u => u.UserName.Contains(username)) + .Where(u => u.UserName.ToLower().Contains(username)) .Select(x => new UserWithoutDetails { Id = x.Id, diff --git a/src/EventHub.Domain/Events/Event.cs b/src/EventHub.Domain/Events/Event.cs index d5b9a86..ddc8758 100644 --- a/src/EventHub.Domain/Events/Event.cs +++ b/src/EventHub.Domain/Events/Event.cs @@ -179,7 +179,8 @@ namespace EventHub.Events DateTime startTime, DateTime endTime, string description, - string language) + string language, + ICollection speakerUserIds) { if (startTime > endTime) { @@ -192,7 +193,7 @@ namespace EventHub.Events } var track = GetTrack(trackId); - track.AddSession(sessionId, title, startTime, endTime, description, language); + track.AddSession(sessionId, title, startTime, endTime, description, language, speakerUserIds); return this; } diff --git a/src/EventHub.Domain/Events/Session.cs b/src/EventHub.Domain/Events/Session.cs index 290b4fb..ea27f28 100644 --- a/src/EventHub.Domain/Events/Session.cs +++ b/src/EventHub.Domain/Events/Session.cs @@ -34,7 +34,8 @@ namespace EventHub.Events DateTime startTime, DateTime endTime, string description, - string language) + string language, + ICollection speakerUserIds) : base(id) { TrackId = trackId; @@ -42,7 +43,9 @@ namespace EventHub.Events SetDescription(description); SetTime(startTime, endTime); SetLanguage(language); + Speakers = new Collection(); + AddSpeakers(speakerUserIds); } public Session SetTitle(string title) @@ -75,5 +78,29 @@ namespace EventHub.Events return this; } + + internal Session AddSpeakers(ICollection userIds) + { + foreach (var userId in userIds) + { + AddSpeaker(userId); + } + + return this; + } + + internal Session AddSpeaker(Guid userId) + { + Speakers.Add(new Speaker(Id, userId)); + + return this; + } + + internal Session RemoveSpeaker(Guid userId) + { + Speakers.RemoveAll(t => t.UserId == userId); + + return this; + } } } diff --git a/src/EventHub.Domain/Events/Track.cs b/src/EventHub.Domain/Events/Track.cs index bbe90b5..1c3783e 100644 --- a/src/EventHub.Domain/Events/Track.cs +++ b/src/EventHub.Domain/Events/Track.cs @@ -42,7 +42,8 @@ namespace EventHub.Events DateTime startTime, DateTime endTime, string description, - string language) + string language, + ICollection speakerUserIds) { if (startTime > endTime) { @@ -58,7 +59,7 @@ namespace EventHub.Events } } - Sessions.Add(new Session(sessionId, Id, title, startTime, endTime, description, language)); + Sessions.Add(new Session(sessionId, Id, title, startTime, endTime, description, language, speakerUserIds)); return this; } diff --git a/src/EventHub.HttpApi/Controllers/Users/UserController.cs b/src/EventHub.HttpApi/Controllers/Users/UserController.cs index 9147c3a..a95f94b 100644 --- a/src/EventHub.HttpApi/Controllers/Users/UserController.cs +++ b/src/EventHub.HttpApi/Controllers/Users/UserController.cs @@ -32,6 +32,7 @@ namespace EventHub.Controllers.Users [Route("contains/{username}")] public async Task> GetListByUserName(string username) { + username = username.Trim().ToLower(); return await _userAppService.GetListByUserName(username); } } diff --git a/src/EventHub.Web/Pages/Events/Components/CreateEventArea/_addSessionSection.cshtml b/src/EventHub.Web/Pages/Events/Components/CreateEventArea/_addSessionSection.cshtml index 62b3eb5..d88b94e 100644 --- a/src/EventHub.Web/Pages/Events/Components/CreateEventArea/_addSessionSection.cshtml +++ b/src/EventHub.Web/Pages/Events/Components/CreateEventArea/_addSessionSection.cshtml @@ -101,36 +101,28 @@