Dodano dodawanie postów i odpowiedzi

This commit is contained in:
Bartosz Chyzy 2018-12-02 19:28:23 +01:00
parent df0924ff0b
commit 8f5b4ce308
10 changed files with 218 additions and 6 deletions

View File

@ -1,12 +1,11 @@
using System; using System;
using System.Collections.Generic;
using System.Data.Entity; using System.Data.Entity;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
using Forum.DataAccessLayer; using Forum.DataAccessLayer;
using Forum.DataAccessLayer.Models; using Forum.DataAccessLayer.Models;
using Forum.ViewModels;
namespace Forum.Controllers namespace Forum.Controllers
{ {
@ -28,7 +27,7 @@ namespace Forum.Controllers
return View(categories); return View(categories);
} }
[Route("Home/Category/{name}")] [System.Web.Mvc.Route("Home/Category/{name}")]
public async Task<ActionResult> Category(string name) public async Task<ActionResult> Category(string name)
{ {
var category = await _dbContext.Categories.Include(q => q.Questions) var category = await _dbContext.Categories.Include(q => q.Questions)
@ -40,7 +39,7 @@ namespace Forum.Controllers
return View(category); return View(category);
} }
[Route("Home/Question/{id}")] [System.Web.Mvc.Route("Home/Question/{id}")]
public async Task<ActionResult> Question(int id) public async Task<ActionResult> Question(int id)
{ {
var question = await _dbContext.Questions.Include(a=>a.Answers.Select(p=>p.Professional)) var question = await _dbContext.Questions.Include(a=>a.Answers.Select(p=>p.Professional))
@ -48,5 +47,71 @@ namespace Forum.Controllers
return View(question); 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});
}
} }
} }

View File

@ -63,6 +63,9 @@
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.5.1\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath> <HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.5.1\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Web.DynamicData" /> <Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" /> <Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" /> <Reference Include="System.Web.ApplicationServices" />
@ -72,6 +75,9 @@
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.Helpers.dll</HintPath> <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.Helpers.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.7\lib\net45\System.Web.Mvc.dll</HintPath> <HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.7\lib\net45\System.Web.Mvc.dll</HintPath>
</Reference> </Reference>
@ -179,6 +185,8 @@
<Compile Include="Models\ManageViewModels.cs" /> <Compile Include="Models\ManageViewModels.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" /> <Compile Include="Startup.cs" />
<Compile Include="ViewModels\AddAnswerViewModel.cs" />
<Compile Include="ViewModels\AddQuestionViewModel.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Content\bootstrap-theme.css" /> <Content Include="Content\bootstrap-theme.css" />
@ -269,10 +277,11 @@
<Content Include="Scripts\popper.js.map" /> <Content Include="Scripts\popper.js.map" />
<Content Include="Scripts\popper-utils.min.js.map" /> <Content Include="Scripts\popper-utils.min.js.map" />
<Content Include="Scripts\popper-utils.js.map" /> <Content Include="Scripts\popper-utils.js.map" />
<Content Include="Views\Home\AddQuestion.cshtml" />
<Content Include="Views\Home\AddAnswer.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="App_Data\" /> <Folder Include="App_Data\" />
<Folder Include="ViewModels\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />

View File

@ -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; }
}
}

View File

@ -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<Category> Categories { get; set; }
}
}

View File

@ -0,0 +1,26 @@
@model Forum.ViewModels.AddAnswerViewModel
@{
ViewBag.Title = "AddAnswer";
}
<h2>AddAnswer</h2>
@using (Html.BeginForm("AddAnswer", "Home", FormMethod.Post))
{
<div class="form-group">
@Html.LabelFor(model => model.Nick, new { @class = "label-form" })
@Html.EditorFor(model => model.Nick, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Content, new { @class = "label-form" })
@Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.HiddenFor(model=>model.QuestionId)
<input type="submit" value="Zgłoś" />
}

View File

@ -0,0 +1,38 @@
@model Forum.ViewModels.AddQuestionViewModel
@{
ViewBag.Title = "AddQuestion";
}
<h2>AddQuestion</h2>
@using (Html.BeginForm("AddQuestion", "Home", FormMethod.Post))
{
<div class="form-group">
@Html.LabelFor(model => model.Title, new { @class = "label-form" })
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Content, new { @class = "label-form" })
@Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Nick, new { @class = "label-form" })
@Html.EditorFor(model => model.Nick, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, new { @class = "label-form" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.DropDownListFor(x=>x.CategoryId,new SelectList(Model.Categories,"Id", "CategoryName"))
<input type="submit" value="Zgłoś"/>
}

View File

@ -6,6 +6,8 @@
<h2>@Model.CategoryName</h2> <h2>@Model.CategoryName</h2>
@Html.ActionLink("Dodaj pytanie", "AddQuestion", "Home",new {category=Model.CategoryName},null)
<ul> <ul>
@{ @{
foreach (var question in Model.Questions) foreach (var question in Model.Questions)

View File

@ -4,4 +4,25 @@
ViewBag.Title = @Model.Title; ViewBag.Title = @Model.Title;
} }
@Model.PostDate
<h2>@Model.Title</h2> <h2>@Model.Title</h2>
<div>
@Model.Content
</div>
<hr/>
@Html.ActionLink("Dodaj odpowiedź","AddAnswer",new{id=@Model.Id})
<hr/>
<b>Odpowiedzi</b>
<ul>
@{
foreach (var answer in Model.Answers)
{
<li>@answer.Nick @answer.Date @answer.Content </li>
}
}
</ul>

View File

@ -32,7 +32,12 @@
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="managedHandler" /> <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="managedHandler" />
</modules> </modules>
<validation validateIntegratedModeConfiguration="false" /> <validation validateIntegratedModeConfiguration="false" />
</system.webServer> <handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<runtime> <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly> <dependentAssembly>

View File

@ -12,6 +12,8 @@
<package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net461" /> <package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net461" />
<package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.4" targetFramework="net461" /> <package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.4" targetFramework="net461" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" /> <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net461" /> <package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net461" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net461" /> <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net461" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.11" targetFramework="net461" /> <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.11" targetFramework="net461" />