Skip to content

Commit

Permalink
insert view implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Serdar Büyüktemiz committed Apr 10, 2016
1 parent 4848d0c commit 5e489a9
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Source/Project.Business/Mappers/PersonMapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Project.Business.Factories;
using Project.Business.Factories;
using Project.Common.Models.Request;
using Project.Data.Entity;
using Project.Common.Models;
Expand Down
8 changes: 8 additions & 0 deletions Source/Project.Business/PersonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,13 @@ public PersonSelectResponse Select(PersonSelectRequest request)
response.Message = "Person selected.";
return response;
}

public PersonSelectPageResponse SelectPage(PersonSelectPageRequest request)
{
var response = new PersonSelectPageResponse();


return response;
}
}
}
7 changes: 7 additions & 0 deletions Source/Project.Common/Contracts/IPersonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ public interface IPersonService
/// <param name="request"></param>
/// <returns></returns>
PersonSelectResponse Select(PersonSelectRequest request);

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
PersonSelectPageResponse SelectPage(PersonSelectPageRequest request);
}
}
37 changes: 37 additions & 0 deletions Source/Project.Common/Models/Request/PersonSelectPageRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Common.Models.Request
{
public class PersonSelectPageRequest : BaseRequest
{
public string Filter { get; set; }
public int Skip { get; set; }
public int Take { get; set; }

public PersonSelectPageRequest(int skip, int take, string filter)
{
Skip = skip;
Take = take;
Filter = filter;
}

public override bool IsNotValid()
{
if (Skip < 0)
{
Skip = 0;
}

if (Take > 100)
{
Take = 100;
}

return false;
}
}
}
12 changes: 12 additions & 0 deletions Source/Project.Common/Models/Response/PersonSelectPageResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Common.Models.Response
{
public class PersonSelectPageResponse : BaseResponse
{
}
}
2 changes: 2 additions & 0 deletions Source/Project.Common/Project.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@
<Compile Include="Models\PersonModel.cs" />
<Compile Include="Models\Request\BaseRequest.cs" />
<Compile Include="Models\Request\PersonInsertRequest.cs" />
<Compile Include="Models\Request\PersonSelectPageRequest.cs" />
<Compile Include="Models\Request\PersonSelectRequest.cs" />
<Compile Include="Models\Response\BaseResponse.cs" />
<Compile Include="Models\Response\PersonInsertResponse.cs" />
<Compile Include="Models\Response\PersonSelectPageResponse.cs" />
<Compile Include="Models\Response\PersonSelectResponse.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
83 changes: 83 additions & 0 deletions Source/Project.WebClient/Controllers/PersonController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

using Project.Common.Contracts;
using Project.WebClient.ViewModels;
using Project.WebClient.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project.Common.Models.Request;

namespace Project.WebClient.Controllers
{
public class PersonController : BaseController
{
public const string LIST_URL = "/Person/List/";
public const string DETAIL_URL = "/Person/Detail/";

IPersonService _personService;

public PersonController(IPersonService personService)
{
_personService = personService;
}

[HttpGet]
public ViewResult List(int skip = 0, int take = 100, string filter = "")
{
if (skip < 0)
{
skip = 0;
}

if (take > 100)
{
take = 100;
}

if (filter.Length > 10)
{
filter = filter.Substring(0, 10);
}

var request = new PersonSelectPageRequest(skip, take, filter);
var model = _personService.SelectPage(request);

//todo: implement

return View();
}

[HttpGet]
public ActionResult New()
{
return View();
}

[HttpPost, ValidateAntiForgeryToken]
public ActionResult New(PersonViewModel model)
{
if (model == null)
{
return View();
}

var request = PersonMapper.MapRequestFromViewModel(model);
if (request.IsNotValid())
{
model.Message = "Request is not valid!";
return View(model);
}

var response = _personService.Insert(request);
if (response.Status)
{
return Redirect($"{DETAIL_URL}{response.Model.UId}");
}

model.Message = response.Message;
return View(model);
}
}
}
20 changes: 20 additions & 0 deletions Source/Project.WebClient/Mappers/PersonMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Project.Common.Models.Request;
using Project.WebClient.ViewModels;

namespace Project.WebClient.Mappers
{
public static class PersonMapper
{
public static PersonInsertRequest MapRequestFromViewModel(PersonViewModel model)
{
var request = new PersonInsertRequest();

request.FirstName = model.FirstName;
request.LastName = model.LastName;
request.Email = model.Email;
request.Phone = model.Phone;

return request;
}
}
}
7 changes: 7 additions & 0 deletions Source/Project.WebClient/Project.WebClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Views\web.config" />
<Content Include="Views\_ViewStart.cshtml" />
<Content Include="Views\Person\New.cshtml" />
<Content Include="Views\Person\Detail.cshtml" />
<Content Include="Views\Person\List.cshtml" />
<Content Include="Views\Person\Edit.cshtml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
Expand All @@ -158,14 +162,17 @@
<ItemGroup>
<Compile Include="Controllers\BaseController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\PersonController.cs" />
<Compile Include="Filters\JournalAttribute.cs" />
<Compile Include="Filters\LogAttribute.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Helpers\AuthHelper.cs" />
<Compile Include="Helpers\HtmlHelpers.cs" />
<Compile Include="Mappers\PersonMapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\BaseModel.cs" />
<Compile Include="ViewModels\PersonViewModel.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions Source/Project.WebClient/Statics/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
font-family: Consolas;
}

.sitename {
margin-top: 13px !important;
margin-bottom: -8px !important;
text-align: left;
form input {
width: 55%!important;
margin-bottom:13px!important;
}
7 changes: 7 additions & 0 deletions Source/Project.WebClient/ViewModels/BaseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Project.WebClient.ViewModels
{
public class BaseModel
{
public string Message { get; set; }
}
}
13 changes: 6 additions & 7 deletions Source/Project.WebClient/ViewModels/PersonViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Project.WebClient.ViewModels
namespace Project.WebClient.ViewModels
{
public class PersonViewModel
public class PersonViewModel : BaseModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
}
3 changes: 2 additions & 1 deletion Source/Project.WebClient/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

<a href="/Person/List" class="btn btn-primary">Show People</a>

2 changes: 2 additions & 0 deletions Source/Project.WebClient/Views/Person/Detail.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<a href="/Person/New" class="btn btn-primary pull-right">Add New Person</a>

2 changes: 2 additions & 0 deletions Source/Project.WebClient/Views/Person/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<a href="/Person/New" class="btn btn-primary pull-right">Add New Person</a>

2 changes: 2 additions & 0 deletions Source/Project.WebClient/Views/Person/List.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<a href="/Person/New" class="btn btn-primary pull-right">Add New Person</a>

35 changes: 35 additions & 0 deletions Source/Project.WebClient/Views/Person/New.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@model PersonViewModel
@{

ViewBag.PageTitle = "New Person Form";
}

<h2>@ViewBag.PageTitle</h2>
<br />

<form method="post">

@if (!string.IsNullOrWhiteSpace(Model?.Message))
{
<div class="alert alert-danger">
<strong>Error!</strong> @Model.Message
</div>
}

<label for="FirstName">First Name</label>
<input type="text" name="FirstName" id="FirstName" class="form-control" />

<label for="LastName">Last Name</label>
<input type="text" name="LastName" id="LastName" class="form-control" />

<label for="Email">Email</label>
<input type="email" name="Email" id="Email" class="form-control" />

<label for="Phone">Phone</label>
<input type="text" name="Phone" id="Phone" class="form-control" />

<br/>
@Html.AntiForgeryToken()
<button type="submit" class="btn btn-primary">Save</button>
</form>

2 changes: 1 addition & 1 deletion Source/Project.WebClient/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<body>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center sitename">
<div class="col-lg-12">
<h1>
<a href="/">Sample Project</a>
</h1>
Expand Down
5 changes: 3 additions & 2 deletions Test/Project.Business.Test/Helpers/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Project.Common.Models.Request;
using System;
using Project.Common.Models.Request;

namespace Project.Business.Test.Helpers
{
Expand All @@ -19,6 +20,6 @@ public static PersonSelectRequest GetPersonSelectRequest(string uid)
var request = new PersonSelectRequest();
request.UId = uid;
return request;
}
}
}
}
16 changes: 16 additions & 0 deletions Test/Project.Business.Test/PersonServiceUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,21 @@ public void PersonService_Insert_ReturnsPersonInsertResponse()
//assert
Assert.IsType<PersonInsertResponse>(response);
}

[Fact]
public void PersonService_Select_ReturnsPersonSelectResponse()
{
//arrange
var request = TestHelper.GetPersonSelectRequest("test");

var repository = A.Fake<PersonRepository>();
var service = new PersonService(repository);

//act
var response = service.Select(request);

//assert
Assert.IsType<PersonSelectResponse>(response);
}
}
}

0 comments on commit 5e489a9

Please sign in to comment.