public async Task <IActionResult> CreateThread(CreateThreadRequest request)
        {
            // Returns error if either parameter is null
            if (request.Title == null || request.Content == null)
            {
                return(BadRequest("Title and content cannot be null"));
            }

            // Creates and adds thread to database
            User user = await _repository.GetUserAsync(User);

            Thread thread = new Thread
            {
                Title      = request.Title,
                Content    = request.Content,
                DatePosted = DateTime.Now,
                User       = user
            };
            await _repository.AddThreadAsync(thread);

            await _repository.SaveChangesAsync();

            // Returns JSON response
            return(Json(new ApiThread(thread)));
        }
        public async Task <IActionResult> CreateThread(string title, string content)
        {
            // Redirects if the title or content are empty
            if (title == null || content == null)
            {
                return(Redirect("/Thread/Create"));
            }

            // Creates and adds thread to database
            DateTime time   = DateTime.Now;
            int      userID = Tools.GetUserID(User);

            Thread thread = new Thread()
            {
                Title      = title,
                Content    = content,
                DatePosted = time,
                UserID     = userID
            };

            await _repository.AddThreadAsync(thread);

            await _repository.SaveChangesAsync();

            // Redirects to thread
            int threadID = thread.ID;

            return(Redirect("/Thread?id=" + threadID));
        }
Exemple #3
0
        public RepositoryTest()
        {
            // Creates ApplicationDbContext from in memory database
            DbContextOptions <ApplicationDbContext> options = new DbContextOptionsBuilder <ApplicationDbContext>()
                                                              .UseSqlite(CreateInMemory()).Options;

            _connection = RelationalOptionsExtension.Extract(options).Connection;
            ApplicationDbContext context = new ApplicationDbContext(options);

            context.Database.EnsureCreated();

            // Creates repository and adds data
            repository = new SimpleForumRepository(context, null, null, null);
            repository.AddUserAsync(new User()
            {
                Username = "******", Password = "******", Email = "*****@*****.**"
            }).Wait();
            repository.SaveChangesAsync().Wait();
            repository.AddThreadAsync(new Thread()
            {
                Title = "first thread", Content = "Thread content", UserID = 1
            }).Wait();
            repository.SaveChangesAsync().Wait();
        }