Example #1
0
        public HttpResponseMessage Detailed(int id)
        {
            var tag = _tagService.GetTagById(id);

            if (tag == null)
            {
                return(PageHelper.toJson(PageHelper.ReturnValue(false, "该数据不存在!")));
            }
            var content = tag.Content;
            List <ContentModel> list;

            if (content == null)
            {
                list = new List <ContentModel>();
            }
            else
            {
                list = (from c in content
                        select new ContentModel
                {
                    Id = c.Id,
                    Title = c.Title,
                    AddUser = c.Adduser,
                    Status = c.Status,
                    Channel = c.Channel.Name
                }).ToList();
            }
            var newModel = new TagDetailModel {
                Tag = new TagModel {
                    Id = tag.Id, Tag = tag.Tag
                },
                Contents = list
            };

            return(PageHelper.toJson(newModel));
        }
Example #2
0
        public async Task <ResponseModel <UpdateQuestionModel> > Handle(UpdateQuestionCommand request, CancellationToken cancellationToken)
        {
            var redisKey  = $"QuestionsByTenantId{request.TenantId}";
            var updatedAt = DateTimeOffset.Now;

            var questionListByTenantId = _context.Questions
                                         .Where(x => x.TenantId == request.TenantId)
                                         .Include(x => x.QuestionTags)
                                         .ThenInclude(y => y.Tag);

            var updateQuestion = questionListByTenantId.FirstOrDefault(x => x.Id == request.Id);

            using (var transaction = await _context.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    if (updateQuestion == null)
                    {
                        throw new NotFoundException(nameof(Question), request.Id);
                    }

                    updateQuestion.Duration             = request.Duration;
                    updateQuestion.Text                 = request.Text;
                    updateQuestion.QuestionCategoryId   = request.CategoryId;
                    updateQuestion.QuestionDifficultyId = request.DifficultyId;
                    updateQuestion.QuestionTypeId       = request.TypeId;
                    updateQuestion.ContentFileId        = request.ContentFileId;
                    updateQuestion.UpdatedAt            = updatedAt;
                    updateQuestion.UpdatedBy            = request.UpdatedBy;


                    if (request.TagsIdList != null)
                    {
                        _context.QuestionTags.RemoveRange(updateQuestion.QuestionTags);
                        foreach (var TagId in request.TagsIdList)
                        {
                            updateQuestion.QuestionTags.Add(new QuestionTag
                            {
                                QuestionId = updateQuestion.Id,
                                TagId      = TagId
                            });
                        }
                    }


                    _context.Questions.Update(updateQuestion);
                    await _context.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    await _cacheService.RedisCacheUpdateAsync(redisKey,
                                                              _ => questionListByTenantId.ToList()
                                                              , cancellationToken);
                }
                catch (DbUpdateException ex) when((ex.InnerException is SqlException sqlException && (sqlException.Number == 2627 || sqlException.Number == 2601)) ||
                                                  (ex.InnerException is SqliteException sqliteException && sqliteException.SqliteErrorCode == 19))
                {
                    transaction.Rollback();
                    throw new ObjectAlreadyExistsException(nameof(Question), request.Text);
                }
                catch (NotFoundException)
                {
                    transaction.Rollback();
                    throw;
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw new TransactionException();
                }
            }

            var updateQuestionModel = new UpdateQuestionModel(updateQuestion.Id,
                                                              updateQuestion.Text,
                                                              updateQuestion.Duration,
                                                              updateQuestion.QuestionDifficultyId,
                                                              updateQuestion.QuestionCategoryId,
                                                              updateQuestion.QuestionTypeId,
                                                              updateQuestion.QuestionTags.Select(x => TagDetailModel.Create(x.Tag)).ToList(),
                                                              updateQuestion.ContentFileId,
                                                              request.UpdatedBy,
                                                              updatedAt);

            return(new ResponseModel <UpdateQuestionModel>(updateQuestionModel));
        }