Exemple #1
0
        public IHttpActionResult AddTopic([FromBody] NewTopic newTopic)
        {
            if (newTopic is null)
            {
                return(NotFound());
            }

            // Generate Topic & Add to database
            var NewTopic = new Topic();

            NewTopic.TopicId             = Guid.NewGuid();
            NewTopic.TopicCompletionTime = newTopic.TopicCompletionTime;
            NewTopic.TopicDescription    = newTopic.TopicDescription;
            NewTopic.TopicGains          = newTopic.TopicGains;
            NewTopic.TopicRewards        = newTopic.TopicRewards;
            NewTopic.TopicTitle          = newTopic.TopicTitle;
            NewTopic.TopicVideo          = newTopic.TopicIntroVideo;
            NewTopic.TopicXpReward       = newTopic.TopicXpReward;
            NewTopic.TopicTag            = newTopic.TopicTag;
            NewTopic.TopicPreviewImage   = newTopic.TopicPreviewImage;

            // Add to database
            WmData.Topics.Add(NewTopic);
            WmData.SaveChanges();

            // Return success message
            return(Ok("Topic Added"));
        }
Exemple #2
0
        private async Task <IActionResult> AddTopicToDb(NewTopic newTopic)
        {
            var topicToAdd = new Topic
            {
                Title = newTopic.Title,
                Text  = newTopic.Text,
                Date  = DateTime.Now
            };

            var uri = UriFactory.CreateDocumentCollectionUri(_cosmosConfig.DefaultDb,
                                                             _cosmosConfig.TopicsCollection);

            try
            {
                var savedDoc = await _client.CreateDocumentAsync(uri, topicToAdd);

                //Topic savedTopic = (dynamic)savedDoc;

                var notificationMessage = $"Topic added: {topicToAdd.Title}";
                await _notifcationService.SendNotificationToSubscribers(notificationMessage, Request.GetBaseUrl());

                //change to created
                return(Ok());
            }
            catch (DocumentClientException de)
            {
                return(BadRequest(de.Message));
            }
        }
Exemple #3
0
        public IHttpActionResult EditTopic([FromBody] NewTopic newTopic)
        {
            if (newTopic is null)
            {
                return(NotFound());
            }

            // Generate Topic & Add to database
            Guid NewGuid  = new Guid(newTopic.TopicId);
            var  NewTopic = WmData.Topics.FirstOrDefault(t => t.TopicId == NewGuid);

            NewTopic.TopicCompletionTime = newTopic.TopicCompletionTime;
            NewTopic.TopicDescription    = newTopic.TopicDescription;
            NewTopic.TopicGains          = newTopic.TopicGains;
            NewTopic.TopicRewards        = newTopic.TopicRewards;
            NewTopic.TopicTitle          = newTopic.TopicTitle;
            NewTopic.TopicVideo          = newTopic.TopicIntroVideo;
            NewTopic.TopicXpReward       = newTopic.TopicXpReward;

            // Add to database
            WmData.SaveChanges();

            // Return success message
            return(Ok("Topic Edited"));
        }
        public async Task <IActionResult> Post([FromBody] NewTopic topic)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var category = _categoryService.GetById(topic.CategoryId);
                    var user     = _userManager.FindByIdAsync(topic.User.Id);

                    topic.Category = category;
                    topic.User     = await user;

                    if (_context.Topics.Any(t => t.Title == topic.Title))
                    {
                        return(BadRequest("Topic with this title already exists"));
                    }
                    await _topicService.Add(topic);

                    await _context.SaveChangesAsync();
                }
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                return(BadRequest("Unable to save changes."));
            }
            return(CreatedAtAction(nameof(Get), new { id = topic.Id }, topic));
        }
        public async Task <IActionResult> EditTopicAsync(int id, NewTopic topic)
        {
            var topicToUpdate = _topicService.GetById(id);

            if (await TryUpdateModelAsync(topicToUpdate,
                                          "",
                                          t => t.Title))
            {
                if (_context.Topics.Any(t => t.Title == topicToUpdate.Title))
                {
                    ModelState.AddModelError("Title", "Topic with this title already exists");
                    return(View(topic));
                }
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("TopicsByCategory", "Topics", new { id = topicToUpdate.Category.Id }));
                }
                catch (DataException /* dex */)
                {
                    //Log the error (uncomment dex variable name and add a line here to write a log.
                    ModelState.AddModelError("", "Unable to save changes. Try again...");
                }
            }
            return(View(topicToUpdate));
        }
Exemple #6
0
        public async Task <IActionResult> AddTopic([FromBody] NewTopic newTopic)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid topic"));
            }

            return(await AddTopicToDb(newTopic));
        }
Exemple #7
0
 public void Insert(NewTopic newcontact)
 {
     using (var dbConn = new SQLiteConnection(App.DB_PATH))
     {
         dbConn.RunInTransaction(() =>
         {
             dbConn.Insert(newcontact);
         });
     }
 }
        private CreatedTopic CreateTopic(int?categoryId, string title = "Test topic: ")
        {
            var newTopic = new NewTopic
            {
                Content =
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut posuere eleifend nulla, vel interdum elit. Aenean auctor, libero in consectetur sollicitudin, dolor massa pharetra nulla, et mollis justo turpis nec tortor. Nullam lorem nunc, cursus ac pretium in, auctor eu nisi. Nullam quis lacus quam. Curabitur vel aliquet erat, sed vestibulum ante. Vivamus consequat lorem eget gravida gravida. Aenean quis arcu ut ligula facilisis aliquet. Vestibulum rutrum magna elit. Etiam sit amet malesuada quam, nec tempor mi. Nullam eu mauris dui. Sed at mi sit amet justo ultrices laoreet non eget urna. Proin felis ex, finibus et auctor a, rutrum nec elit.",
                Title      = title + Guid.NewGuid(),
                CategoryID = categoryId
            };
            var response = _api.CreateTopic(newTopic);

            return(response);
        }
        public async Task <IActionResult> Put(int id, [FromBody] NewTopic topic)
        {
            var topicToUpdate = _topicService.GetById(id);

            if (_context.Topics.Any(t => t.Title == topic.Title))
            {
                return(BadRequest("Topic with this title already exists"));
            }
            topicToUpdate.Title = topic.Title;
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { id = topicToUpdate.Id }, topicToUpdate));
        }
        public IActionResult Edit(int id)
        {
            var topic = _topicService.GetById(id);
            var model = new NewTopic
            {
                CategoryId = topic.Category.Id,
                Title      = topic.Title,
            };

            if (topic.User.UserName != User.Identity.Name)
            {
                return(NotFound());
            }

            if (topic == null)
            {
                return(NotFound());
            }

            return(View(model));
        }
        public async Task <IActionResult> CreateTopicandPost(int id, NewTopicandPost topicandpost, NewTopic topic, NewPost post)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var category = _categoryService.GetById(id);
                    var userId   = _userManager.GetUserId(User);
                    var user     = _userManager.FindByIdAsync(userId);

                    topic.Category   = category;
                    topic.User       = await user;
                    topic.CategoryId = id;

                    if (_context.Topics.Any(t => t.Title == topic.Title))
                    {
                        ModelState.AddModelError("Topic.Title", "Topic with this title already exists");
                        return(View(topicandpost));
                    }
                    await _topicService.Add(topic);

                    post.User  = await user;
                    post.Topic = topic;

                    if (_context.Posts.Any(p => p.Content == post.Content))
                    {
                        ModelState.AddModelError("Post.Content", "Post with this content already exist");
                        return(View(topicandpost));
                    }
                    await _postService.Add(post);

                    return(RedirectToAction("PostsByTopic", "Posts", new { id = topic.Id }));
                }
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes.");
            }
            return(View(topicandpost));
        }
Exemple #12
0
 public void Visit(NewTopic topic)
 {
 }
Exemple #13
0
 public static CreatedTopic CreateTopic(this DiscourseApi api, NewTopic data, string username = DefaultUsername)
 {
     return(api.ExecuteRequest <CreatedTopic>("/posts", Method.POST, true, username, null, data));
 }