diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 9baefc31..b359864f 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -7,6 +7,9 @@ using RMuseum.Models.GanjoorAudio.ViewModels; using RMuseum.Models.GanjoorIntegration.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Generic; +using RSecurityBackend.Services; +using System; +using System.Linq; using System.Net; using System.Threading.Tasks; @@ -249,6 +252,37 @@ namespace RMuseum.Controllers return Ok(res.Result); } + /// + /// suggest song for poem + /// + /// + /// + + [HttpPost] + [Route("song")] + [Authorize] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PoemMusicTrackViewModel))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.Forbidden)] + public async Task SuggestSong([FromBody] PoemMusicTrackViewModel song) + { + Guid userId = + new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); + Guid sessionId = + new Guid(User.Claims.FirstOrDefault(c => c.Type == "SessionId").Value); + RServiceResult sessionCheckResult = await _appUserService.SessionExists(userId, sessionId); + if (!string.IsNullOrEmpty(sessionCheckResult.ExceptionString)) + { + return StatusCode((int)HttpStatusCode.Forbidden); + } + + var res = + await _ganjoorService.SuggestSong(userId, song); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(res.Result); + } + /// /// get a random poem from hafez /// @@ -327,13 +361,20 @@ namespace RMuseum.Controllers protected readonly IGanjoorService _ganjoorService; + /// + /// IAppUserService instance + /// + protected IAppUserService _appUserService; + /// /// constructor /// /// - public GanjoorController(IGanjoorService ganjoorService) + /// + public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService) { _ganjoorService = ganjoorService; + _appUserService = appUserService; } } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 24839de6..87275924 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -707,6 +707,13 @@ + + + suggest song for poem + + + + get a random poem from hafez @@ -736,11 +743,17 @@ Ganjoor Service - + + + IAppUserService instance + + + constructor + @@ -4172,7 +4185,7 @@ poem id - + a @@ -5509,6 +5522,14 @@ + + + suggest song + + + + + get a random poem from hafez @@ -6325,6 +6346,14 @@ + + + suggest song + + + + + get a random poem from hafez diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index a347fa27..8c87a922 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -3,6 +3,7 @@ using RMuseum.Models.Ganjoor.ViewModels; using RMuseum.Models.GanjoorAudio.ViewModels; using RMuseum.Models.GanjoorIntegration.ViewModels; using RSecurityBackend.Models.Generic; +using System; using System.Threading.Tasks; namespace RMuseum.Services @@ -105,6 +106,14 @@ namespace RMuseum.Services /// Task> GetPoemImages(int id); + /// + /// suggest song + /// + /// + /// + /// + Task> SuggestSong(Guid userId, PoemMusicTrackViewModel song); + /// /// get a random poem from hafez /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index e1bae36d..c4005aa1 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -721,6 +721,59 @@ namespace RMuseum.Services.Implementation } } + /// + /// suggest song + /// + /// + /// + /// + public async Task> SuggestSong(Guid userId, PoemMusicTrackViewModel song) + { + try + { + var alreadySuggestedSong = await _context.GanjoorPoemMusicTracks.Where(t => t.PoemId == song.PoemId && t.TrackType == song.TrackType && t.TrackUrl == song.TrackUrl).FirstOrDefaultAsync(); + if(alreadySuggestedSong != null) + { + return new RServiceResult(null, "این آهنگ پیشتر برای این شعر پیشنهاد داده شده است."); + } + + + + var sug = + new PoemMusicTrack() + { + TrackType = song.TrackType, + PoemId = song.PoemId, + ArtistName = song.ArtistName, + ArtistUrl = song.ArtistUrl, + AlbumName = song.AlbumName, + AlbumUrl = song.AlbumUrl, + TrackName = song.TrackName, + TrackUrl = song.TrackUrl, + SuggestedById = userId + }; + + GanjoorSinger singer = await _context.GanjoorSingers.Where(s => s.Url == song.ArtistUrl).FirstOrDefaultAsync(); + if (singer != null) + { + sug.SingerId = singer.Id; + } + + _context.GanjoorPoemMusicTracks.Add + ( + sug + ); + + await _context.SaveChangesAsync(); + song.Id = sug.Id; + return new RServiceResult(song); + } + catch(Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// get a random poem from hafez /// @@ -1519,6 +1572,7 @@ namespace RMuseum.Services.Implementation /// protected readonly IBackgroundTaskQueue _backgroundTaskQueue; + /// /// constructor