public async Task <Result <PlaylistDto> > Handle(AddPlaylistCommand command, CancellationToken cancellationToken)
    {
        //Check if there is a global or guild playlist that has the same name.
        if (await _context.Playlists.AnyAsync(playlist => (playlist.IsGlobal || playlist.GuildId == command.GuildId) && playlist.Name.ToLower() == command.Name.ToLower(), cancellationToken))
        {
            return(await Result <PlaylistDto> .FailAsync("There already is a playlist with the same name"));
        }

        if (command.GuildId != null)
        {
            var guild = await _context.Guilds.Include(x => x.GuildSetting).Include(x => x.GuildPlaylists).FirstOrDefaultAsync(guild => guild.Id == command.GuildId, cancellationToken);

            if (guild == null)
            {
                return(await Result <PlaylistDto> .FailAsync("Guild not found"));
            }

            if (guild.GuildPlaylists.Count >= guild.GuildSetting.MaxPlaylists)
            {
                return(await Result <PlaylistDto> .FailAsync("Max playlists reached"));
            }
        }


        var playlist = _mapper.Map <Playlist>(command);
        await _context.Playlists.AddAsync(playlist, cancellationToken);

        await _context.SaveChangesAsync(cancellationToken);

        var dto = _mapper.Map <PlaylistDto>(playlist);

        return((await Result <PlaylistDto> .SuccessAsync(dto, "Playlist created")) as Result <PlaylistDto>);
    }
        public async Task <IActionResult> CreatePlaylist([FromBody] AddPlaylistCommand command)
        {
            command.PlaylistId = Guid.NewGuid();
            command.UserId     = _accountService.UserId;

            await _mediator.Send(command);

            return(Ok());
        }
 public UserPlaylistViewModel()
 {
     this.PathSelector = new PathSelector();
     this.PathSelector.SetPath(path => this._currenItem.Path = path);
     _playListPersistence     = new PlaylistRepository(SqlConnector.GetDefaultConnection());
     _playListItemPersistence = new PlaylistItemRepository(SqlConnector.GetDefaultConnection());
     _playlistCollection.Clear();
     GetListsFromRepository();
     AddItemCommand = new AddPlaylistCommand(this);
     RemovePlaylist = new RemovePlaylist(this);
 }
示例#4
0
 public async Task <IActionResult> Add(AddPlaylistCommand command)
 {
     return(Ok(await Mediator.Send(command)));
 }