Esempio n. 1
0
        /// <summary>
        /// 取消收藏歌曲
        /// </summary>
        /// <param name="response"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        public async Task CollectionSongCancel([Required] ResponseMessage response, [Required] SongCollectionRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Check arguments.
            if (request.User == null || request.User.Id == null)
            {
                Def.Response.Wrap(response, Def.Response.BadRequsetCode, "请求体未包含系统用户信息,用户未登陆不能进行收藏歌曲操作");
                return;
            }
            if (request.SongId == null)
            {
                Def.Response.Wrap(response, Def.Response.BadRequsetCode, "取消歌曲收藏,其ID不能为空");
                return;
            }
            // Check whether user exist.
            var user = TheUsers.ById(request.User.Id).SingleOrDefault();

            if (user == null)
            {
                Def.Response.Wrap(response, Def.Response.NotFoundCode, Def.User.NotFoundMsg);
                return;
            }
            // Check song exist.
            var songs = TheSongs.ById(request.SongId);

            if (songs.Count() == 0)
            {
                Def.Response.Wrap(response, Def.Response.BadRequsetCode, "你所取消收藏的歌曲不存在");
                return;
            }
            // Check play list exist.
            var playListId = TheUsers.FindPlayListIdByType(request.User.Id, Def.PlayList.Type.Collection).SingleOrDefault();

            if (playListId == null)
            {
                Def.Response.Wrap(response, Def.Response.NotFoundCode, "收藏歌单未找到,可能的原因是创建账号时没有默认创建收藏歌单!");
                return;
            }
            // Check the correlation between songs and songs.
            var playListSong = _RelPlayListSongStore.ById(request.User.Id, playListId, request.SongId).SingleOrDefault();

            if (playListSong == null)
            {
                Def.Response.Append(response, "你所取消收藏的歌曲不在你的收藏歌单中!");  // 修改ResponseMessage方法,添加追加函数?
                return;
            }
            else
            {
                await _RelPlayListSongStore.Delete(request.User.Id, playListSong, cancellationToken);

                response.Append("歌曲取消收藏成功!");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 新建或更新,当第一次创建待办时,传入name和pwd,之后传入id和pwd
        /// </summary>
        /// <param name="response"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        public async Task CreateOrUpdate([Required] ResponseMessage <TodoItemJson> response, [FromBody] ModelRequest <TodoItemJson> request, CancellationToken cancellationToken = default(CancellationToken))
        {
            UserBase user = await UserChecck(response, request.User);

            if (user == null)
            {
                return;
            }
            if (request.model == null)
            {
                response.Wrap(ResponseDefine.BadRequset, "待办项为空,无法执行操作");
                return;
            }
            else
            {
                // 不存在ID,创建操作
                if (string.IsNullOrWhiteSpace(request.model.Id))
                {
                    //  创建User与Todo的关联
                    TodoItem todo = await Store.Create(new TodoItem
                    {
                        Id            = Guid.NewGuid().ToString(),
                        Name          = request.model.Name,
                        Content       = request.model.Content,
                        ExpectTime    = request.model.ExpectTime,
                        _CreateUserId = user.Id
                    }, cancellationToken);

                    response.Extension = Mapper.Map <TodoItemJson>(todo);
                    response.Append("新建待办项成功");
                    return;
                }
                // 待办项ID存在,更新操作
                else
                {
                    // Id 是否有效,是否存在User与Todo的关联以及todo是否存在
                    TodoItem todo = await Store.ById(user.Id, request.model.Id).SingleOrDefaultAsync(cancellationToken);

                    if (todo == null)
                    {
                        response.Wrap(ResponseDefine.NotFound, "找不到该待办项");
                        return;
                    }
                    else
                    {
                        todo.Name       = request.model.Name;
                        todo.Content    = request.model.Content;
                        todo.ExpectTime = request.model.ExpectTime;  // 期望时间,最后的时刻
                        todo.IsComplete = request.model.IsComplete;
                        if (request.model.IsComplete)
                        {
                            todo.ActualTime = DateTime.Now;
                        }
                        todo._UpdateUserId = user.Id;
                        todo._UpdateTime   = user._UpdateTime;
                        response.Extension = Mapper.Map <TodoItemJson>(await Store.Update(todo, cancellationToken));
                        response.Append("更新待办项成功");
                        return;
                    }
                }
            }
        }