From 8f5b4ce308c581b30b16600e55bb7ecf1e9511f0 Mon Sep 17 00:00:00 2001 From: Bartosz Chyzy Date: Sun, 2 Dec 2018 19:28:23 +0100 Subject: [PATCH] =?UTF-8?q?Dodano=20dodawanie=20post=C3=B3w=20i=20odpowied?= =?UTF-8?q?zi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Forum/Forum/Controllers/HomeController.cs | 73 ++++++++++++++++++- Trunk/Server/Forum/Forum/Forum.csproj | 11 ++- .../Forum/ViewModels/AddAnswerViewModel.cs | 17 +++++ .../Forum/ViewModels/AddQuestionViewModel.cs | 27 +++++++ .../Forum/Forum/Views/Home/AddAnswer.cshtml | 26 +++++++ .../Forum/Forum/Views/Home/AddQuestion.cshtml | 38 ++++++++++ .../Forum/Forum/Views/Home/Category.cshtml | 2 + .../Forum/Forum/Views/Home/Question.cshtml | 21 ++++++ Trunk/Server/Forum/Forum/Web.config | 7 +- Trunk/Server/Forum/Forum/packages.config | 2 + 10 files changed, 218 insertions(+), 6 deletions(-) create mode 100644 Trunk/Server/Forum/Forum/ViewModels/AddAnswerViewModel.cs create mode 100644 Trunk/Server/Forum/Forum/ViewModels/AddQuestionViewModel.cs create mode 100644 Trunk/Server/Forum/Forum/Views/Home/AddAnswer.cshtml create mode 100644 Trunk/Server/Forum/Forum/Views/Home/AddQuestion.cshtml diff --git a/Trunk/Server/Forum/Forum/Controllers/HomeController.cs b/Trunk/Server/Forum/Forum/Controllers/HomeController.cs index 8b29490..8574efb 100644 --- a/Trunk/Server/Forum/Forum/Controllers/HomeController.cs +++ b/Trunk/Server/Forum/Forum/Controllers/HomeController.cs @@ -1,12 +1,11 @@ using System; -using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Threading.Tasks; -using System.Web; using System.Web.Mvc; using Forum.DataAccessLayer; using Forum.DataAccessLayer.Models; +using Forum.ViewModels; namespace Forum.Controllers { @@ -28,7 +27,7 @@ namespace Forum.Controllers return View(categories); } - [Route("Home/Category/{name}")] + [System.Web.Mvc.Route("Home/Category/{name}")] public async Task Category(string name) { var category = await _dbContext.Categories.Include(q => q.Questions) @@ -40,7 +39,7 @@ namespace Forum.Controllers return View(category); } - [Route("Home/Question/{id}")] + [System.Web.Mvc.Route("Home/Question/{id}")] public async Task Question(int id) { var question = await _dbContext.Questions.Include(a=>a.Answers.Select(p=>p.Professional)) @@ -48,5 +47,71 @@ namespace Forum.Controllers return View(question); } + + + public ActionResult AddQuestion() + { + var model=new AddQuestionViewModel() + { + Categories = _dbContext.Categories + }; + return View(model); + } + + [System.Web.Mvc.HttpPost] + public ActionResult AddQuestion(AddQuestionViewModel model) + { + var category = _dbContext.Categories.Find(model.CategoryId); + if (category == null) + return HttpNotFound(); + + var question = new Question() + { + Title = model.Title, + Content = model.Content, + ReportersEmail = model.Email, + Nick = model.Nick, + PostDate = DateTime.Now, + IsClosed = false, + Category = category + }; + + _dbContext.Questions.Add(question); + _dbContext.SaveChanges(); + + return RedirectToAction("Question", new {id = question.Id}); + + } + + public ActionResult AddAnswer(int id) + { + var model = new AddAnswerViewModel() + { + QuestionId = id + }; + return View(model); + } + + [System.Web.Mvc.HttpPost] + public ActionResult AddAnswer(AddAnswerViewModel model) + { + var question = _dbContext.Questions.Find(model.QuestionId); + if (question == null) + return HttpNotFound(); + + var answer = new Answer() + { + Content = model.Content, + Nick = model.Nick, + Date = DateTime.Now + }; + + question.Answers.Add(answer); + _dbContext.SaveChanges(); + + return RedirectToAction("Question", new {id = model.QuestionId}); + } + + } } \ No newline at end of file diff --git a/Trunk/Server/Forum/Forum/Forum.csproj b/Trunk/Server/Forum/Forum/Forum.csproj index 4cb3a67..5083301 100644 --- a/Trunk/Server/Forum/Forum/Forum.csproj +++ b/Trunk/Server/Forum/Forum/Forum.csproj @@ -63,6 +63,9 @@ ..\packages\System.Diagnostics.DiagnosticSource.4.5.1\lib\net46\System.Diagnostics.DiagnosticSource.dll + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + @@ -72,6 +75,9 @@ ..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.Helpers.dll + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll + ..\packages\Microsoft.AspNet.Mvc.5.2.7\lib\net45\System.Web.Mvc.dll @@ -179,6 +185,8 @@ + + @@ -269,10 +277,11 @@ + + - diff --git a/Trunk/Server/Forum/Forum/ViewModels/AddAnswerViewModel.cs b/Trunk/Server/Forum/Forum/ViewModels/AddAnswerViewModel.cs new file mode 100644 index 0000000..09866e4 --- /dev/null +++ b/Trunk/Server/Forum/Forum/ViewModels/AddAnswerViewModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace Forum.ViewModels +{ + public class AddAnswerViewModel + { + public int QuestionId { get; set; } + + public string Content { get; set; } + + public string Nick { get; set; } + + } +} \ No newline at end of file diff --git a/Trunk/Server/Forum/Forum/ViewModels/AddQuestionViewModel.cs b/Trunk/Server/Forum/Forum/ViewModels/AddQuestionViewModel.cs new file mode 100644 index 0000000..ab230f6 --- /dev/null +++ b/Trunk/Server/Forum/Forum/ViewModels/AddQuestionViewModel.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Web; +using Forum.DataAccessLayer.Models; + +namespace Forum.ViewModels +{ + public class AddQuestionViewModel + { + public string Title { get; set; } + + public string Content { get; set; } + + [EmailAddress] + public string Email { get; set; } + + public string Nick { get; set; } + + public int CategoryId { get; set; } + + public IEnumerable Categories { get; set; } + + + } +} \ No newline at end of file diff --git a/Trunk/Server/Forum/Forum/Views/Home/AddAnswer.cshtml b/Trunk/Server/Forum/Forum/Views/Home/AddAnswer.cshtml new file mode 100644 index 0000000..a14fdaa --- /dev/null +++ b/Trunk/Server/Forum/Forum/Views/Home/AddAnswer.cshtml @@ -0,0 +1,26 @@ +@model Forum.ViewModels.AddAnswerViewModel +@{ + ViewBag.Title = "AddAnswer"; +} + +

AddAnswer

+ + +@using (Html.BeginForm("AddAnswer", "Home", FormMethod.Post)) +{ + +
+ @Html.LabelFor(model => model.Nick, new { @class = "label-form" }) + @Html.EditorFor(model => model.Nick, new { htmlAttributes = new { @class = "form-control" } }) +
+ +
+ @Html.LabelFor(model => model.Content, new { @class = "label-form" }) + @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } }) +
+ + @Html.HiddenFor(model=>model.QuestionId) + + +} + diff --git a/Trunk/Server/Forum/Forum/Views/Home/AddQuestion.cshtml b/Trunk/Server/Forum/Forum/Views/Home/AddQuestion.cshtml new file mode 100644 index 0000000..e70c009 --- /dev/null +++ b/Trunk/Server/Forum/Forum/Views/Home/AddQuestion.cshtml @@ -0,0 +1,38 @@ +@model Forum.ViewModels.AddQuestionViewModel +@{ + ViewBag.Title = "AddQuestion"; +} + +

AddQuestion

+ + +@using (Html.BeginForm("AddQuestion", "Home", FormMethod.Post)) +{ + + +
+ @Html.LabelFor(model => model.Title, new { @class = "label-form" }) + @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) +
+ +
+ @Html.LabelFor(model => model.Content, new { @class = "label-form" }) + @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } }) +
+ +
+ @Html.LabelFor(model => model.Nick, new { @class = "label-form" }) + @Html.EditorFor(model => model.Nick, new { htmlAttributes = new { @class = "form-control" } }) +
+ +
+ @Html.LabelFor(model => model.Email, new { @class = "label-form" }) + @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) +
+ + @Html.DropDownListFor(x=>x.CategoryId,new SelectList(Model.Categories,"Id", "CategoryName")) + + +} + + diff --git a/Trunk/Server/Forum/Forum/Views/Home/Category.cshtml b/Trunk/Server/Forum/Forum/Views/Home/Category.cshtml index e7de7c0..15d4085 100644 --- a/Trunk/Server/Forum/Forum/Views/Home/Category.cshtml +++ b/Trunk/Server/Forum/Forum/Views/Home/Category.cshtml @@ -6,6 +6,8 @@

@Model.CategoryName

+@Html.ActionLink("Dodaj pytanie", "AddQuestion", "Home",new {category=Model.CategoryName},null) +
    @{ foreach (var question in Model.Questions) diff --git a/Trunk/Server/Forum/Forum/Views/Home/Question.cshtml b/Trunk/Server/Forum/Forum/Views/Home/Question.cshtml index b7584b1..f1803e7 100644 --- a/Trunk/Server/Forum/Forum/Views/Home/Question.cshtml +++ b/Trunk/Server/Forum/Forum/Views/Home/Question.cshtml @@ -4,4 +4,25 @@ ViewBag.Title = @Model.Title; } +@Model.PostDate +

    @Model.Title

    + +
    + @Model.Content +
    + +
    +@Html.ActionLink("Dodaj odpowiedź","AddAnswer",new{id=@Model.Id}) +
    + +Odpowiedzi +
      + @{ + foreach (var answer in Model.Answers) + { +
    • @answer.Nick @answer.Date @answer.Content
    • + } + } +
    + diff --git a/Trunk/Server/Forum/Forum/Web.config b/Trunk/Server/Forum/Forum/Web.config index 19c8237..a347020 100644 --- a/Trunk/Server/Forum/Forum/Web.config +++ b/Trunk/Server/Forum/Forum/Web.config @@ -32,7 +32,12 @@ - + + + + + + diff --git a/Trunk/Server/Forum/Forum/packages.config b/Trunk/Server/Forum/Forum/packages.config index 1da0d5b..f1953cb 100644 --- a/Trunk/Server/Forum/Forum/packages.config +++ b/Trunk/Server/Forum/Forum/packages.config @@ -12,6 +12,8 @@ + +