public async Task <ActionResult> Create(AddTopicModel model)
        {
            if (ModelState.IsValid)
            {
                var topic = new Topic
                {
                    Name = model.Name
                };

                db.Topics.Add(topic);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> Add([FromBody] AddTopicModel model)
        {
            if (model == null || !ModelState.IsValid)
            {
                return(InvalidRequest());
            }

            var result = await TopicService.Add(model);

            if (result.Success)
            {
                return(Success(result.Data));
            }
            else
            {
                return(Error(result.ErrorMessage));
            }
        }
Exemple #3
0
        public async Task <OperationResult <TopicModel> > Add(AddTopicModel model)
        {
            model.CategoryList = (model.CategoryList ?? new int[0]).Distinct().ToArray();
            model.TagList      = (model.TagList ?? new string[0]).Distinct().ToArray();

            List <Category> categoryEntityList = await BlogContext.Categories.Where(t => model.CategoryList.Contains(t.ID)).ToListAsync();

            List <Tag> tagEntityList = await BlogContext.Tags.Where(t => model.TagList.Contains(t.Keyword)).ToListAsync();

            model.Alias = await this.GenerateAlias(null, model.Alias, model.Title);

            model.Summary = this.GenerateSummary(model.Summary, model.Content);

            foreach (var tag in model.TagList)
            {
                if (!tagEntityList.Any(t => t.Keyword == tag))
                {
                    var tagEntity = new Tag
                    {
                        Keyword = tag
                    };
                    BlogContext.Tags.Add(tagEntity);
                    tagEntityList.Add(tagEntity);
                }
            }

            var topic = new Topic
            {
                Alias        = model.Alias,
                AllowComment = model.AllowComment,
                Content      = model.Content,
                CreateDate   = DateTime.Now,
                CreateUserID = this.ClientManager.CurrentUser.ID,
                EditDate     = model.Date ?? DateTime.Now,
                EditUserID   = 1,
                Status       = model.Status,
                Summary      = model.Summary,
                Title        = model.Title
            };

            BlogContext.Topics.Add(topic);

            List <CategoryTopic> categoryTopicList = categoryEntityList.Select(t => new CategoryTopic
            {
                Category = t,
                Topic    = topic
            }).ToList();

            BlogContext.CategoryTopics.AddRange(categoryTopicList);

            List <TagTopic> tagTopicList = tagEntityList.Select(t => new TagTopic
            {
                Tag   = t,
                Topic = topic
            }).ToList();

            BlogContext.TagTopics.AddRange(tagTopicList);

            await BlogContext.SaveChangesAsync();

            BlogContext.RemoveCategoryCache();
            BlogContext.RemoveTagCache();

            var topicModel = (await this.Transform(topic)).First();

            return(new OperationResult <TopicModel>(topicModel));
        }
Exemple #4
0
        public HttpResponseMessage AddTopic(string from, string to)
        {
            try
            {
                var toList       = ((string.IsNullOrEmpty(to) ? "" : to) + ",").Split(',').Where(t => !string.IsNullOrEmpty(t)).ToList();
                var message      = HttpContext.Current.Request.Form["message"];
                var errorMessage = string.Empty;
                IEnumerable <TopicAttachmentInfo> attachments = null;
                if (HttpContext.Current.Request.Files.Count > 0)
                {
                    var files = Enumerable.Range(0, HttpContext.Current.Request.Files.Count).Select(
                        t =>
                    {
                        HttpPostedFileBase filebase = new HttpPostedFileWrapper(HttpContext.Current.Request.Files[t]);
                        return(filebase);
                    });

                    attachments = docUtils.UploadFiles(from, files, ref errorMessage);
                }

                var resp = TasksService.CreateTopic(new CreateTopicRequest
                {
                    Message     = message,
                    From        = from,
                    To          = to,
                    Attachments = attachments
                });

                var fromUser = UsersService.GetUser(new GetUserRequest {
                    User = from
                });
                if (fromUser.User == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                var content = new AddTopicModel
                {
                    Result  = true,
                    TopicId = resp.TopicId,
                    Message = new TopicModelItem(new TopicMessageInfo
                    {
                        From        = fromUser.User.UserName,
                        To          = "",
                        ImageUrl    = fromUser.User.PhotoPath,
                        When        = DateTime.Now,
                        Message     = message,
                        Attachments = attachments == null ? new List <TopicAttachmentInfo>() : attachments,
                        Status      = TopicStatusType.New
                    }),
                    Replies = new List <TopicMessageInfo>().ToArray()
                };

                var result = Request.CreateResponse(HttpStatusCode.OK);
                var json   = JsonConvert.SerializeObject(
                    content,
                    Formatting.Indented,
                    new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }
                    );
                result.Content = new StringContent(json, Encoding.UTF8, "text/plain");
                return(result);
            }
            catch (Exception e)
            {
                var content = new AddTopicModel {
                    Result = false, ErrorMessage = e.Message
                };
                var json = JsonConvert.SerializeObject(
                    content,
                    Formatting.Indented,
                    new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }
                    );
                var result = Request.CreateResponse(HttpStatusCode.OK);
                result.Content = new StringContent(json, Encoding.UTF8, "text/plain");
                return(result);
            }
        }