Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
Lesson49
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Пасько Виталий
Lesson49
Commits
6e40b765
Commit
6e40b765
authored
Oct 14, 2022
by
Пасько Виталий
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Частично покрыл тестами PhoneController.
parent
a5a0c3ef
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
123 additions
and
12 deletions
+123
-12
Utilities.cs
PhoneStore.Tests/Helpers/Utilities.cs
+52
-0
PhoneStore.sln
PhoneStore.sln
+6
-0
PhonesController.cs
PhoneStore/Controllers/PhonesController.cs
+49
-10
Brand.cs
PhoneStore/Models/Brand.cs
+4
-0
Startup.cs
PhoneStore/Startup.cs
+2
-2
BaseEntity.cs
PhoneStore/ViewModels/BaseEntity.cs
+3
-0
PhoneViewModel.cs
PhoneStore/ViewModels/PhoneViewModels/PhoneViewModel.cs
+7
-0
No files found.
PhoneStore.Tests/Helpers/Utilities.cs
0 → 100644
View file @
6e40b765
using
System.Linq
;
using
Microsoft.AspNetCore.Mvc.Testing
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.DependencyInjection
;
using
PhoneStore.Models
;
using
PhoneStore.ViewModels.PhoneViewModels
;
namespace
PhoneStore.Tests.Helpers
{
public
static
class
Utilities
{
private
const
string
ConnectionString
=
"Server=localhost;Port=54320;database=testDb;Username=postgres;Password=postgres;"
;
public
static
WebApplicationFactory
<
Startup
>
SubstituteDbOnTestDb
()
{
return
new
WebApplicationFactory
<
Startup
>().
WithWebHostBuilder
(
builder
=>
{
builder
.
ConfigureServices
(
services
=>
{
var
dbContextDescriptor
=
services
.
SingleOrDefault
(
s
=>
s
.
ServiceType
==
typeof
(
DbContextOptions
<
MobileContext
>));
services
.
Remove
(
dbContextDescriptor
);
services
.
AddDbContext
<
MobileContext
>(
options
=>
{
options
.
UseNpgsql
(
ConnectionString
);
});
});
});
}
public
static
int
CreateBrandAndReturnBrandId
(
MobileContext
context
)
{
Brand
brand
=
new
Brand
{
Name
=
"Asus"
};
context
.
Brands
.
Add
(
brand
);
context
.
SaveChanges
();
return
brand
.
Id
;
}
public
static
int
CreatePhoneAndReturnPhoneId
(
MobileContext
context
)
{
Phone
phone
=
new
Phone
{
Name
=
"Test"
,
Price
=
3000
M
,
Brand
=
new
Brand
{
Name
=
"Adidas"
}
};
context
.
Phones
.
Add
(
phone
);
context
.
SaveChanges
();
return
phone
.
Id
;
}
}
}
\ No newline at end of file
PhoneStore.sln
View file @
6e40b765
...
...
@@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneStore", "PhoneStore\PhoneStore.csproj", "{32DC0801-63D0-4261-9F07-8DD570B35335}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneStore.Tests", "PhoneStore.Tests\PhoneStore.Tests.csproj", "{3DF8B5C0-BB98-4A2E-A589-5311ECBE546D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
...
...
@@ -12,5 +14,9 @@ Global
{32DC0801-63D0-4261-9F07-8DD570B35335}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32DC0801-63D0-4261-9F07-8DD570B35335}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32DC0801-63D0-4261-9F07-8DD570B35335}.Release|Any CPU.Build.0 = Release|Any CPU
{3DF8B5C0-BB98-4A2E-A589-5311ECBE546D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DF8B5C0-BB98-4A2E-A589-5311ECBE546D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DF8B5C0-BB98-4A2E-A589-5311ECBE546D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DF8B5C0-BB98-4A2E-A589-5311ECBE546D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
PhoneStore/Controllers/PhonesController.cs
View file @
6e40b765
...
...
@@ -54,29 +54,53 @@ namespace PhoneStore.Controllers
return
View
(
model
);
}
// [HttpPost]
// public async Task<IActionResult> Create(PhoneCreateViewModel model)
// {
// try
// {
// if (ModelState.IsValid)
// {
// await _phoneService.CreateAsync(model);
//
// return RedirectToAction("Index");
// }
//
// model.Brands = _db.Brands.ToList();
// return View("Create", model);
// }
// //TODO: Добавить кастомный exception
// catch (FileNotFoundException)
// {
// return RedirectToAction("Error", "Errors", new {statusCode = 666});
// }
// catch(Exception)
// {
// return RedirectToAction("Error", "Errors", new {statusCode = 777});
// }
// }
[
HttpPost
]
public
async
Task
<
IActionResult
>
Create
(
PhoneCreateViewModel
model
)
public
async
Task
<
IActionResult
>
Create
(
[
FromBody
]
PhoneCreateViewModel
model
)
{
try
{
if
(
ModelState
.
IsValid
)
{
await
_phoneService
.
CreateAsync
(
model
);
return
RedirectToAction
(
"Index"
);
return
Ok
();
}
model
.
Brands
=
_db
.
Brands
.
ToList
();
return
V
iew
(
"Create"
,
model
);
return
V
alidationProblem
(
);
}
//TODO: Добавить кастомный exception
catch
(
FileNotFoundException
)
{
return
RedirectToAction
(
"Error"
,
"Errors"
,
new
{
statusCode
=
666
}
);
return
BadRequest
(
);
}
catch
(
Exception
)
{
return
RedirectToAction
(
"Error"
,
"Errors"
,
new
{
statusCode
=
777
}
);
return
BadRequest
(
);
}
}
...
...
@@ -138,19 +162,34 @@ namespace PhoneStore.Controllers
return
RedirectToAction
(
"Index"
);
}
// [HttpGet]
// public IActionResult About(int? phoneId)
// {
// try
// {
// if (!phoneId.HasValue) return RedirectToAction("Error", "Errors", new {statusCode = 777});
// var phoneViewModel = _phoneService.GetById(phoneId.Value);
// return View(phoneViewModel);
// }
// catch (NullReferenceException)
// {
// return RedirectToAction("Error", "Errors", new {statusCode = 777});
// }
// }
[
HttpGet
]
public
IActionResult
About
(
int
?
phoneId
)
{
try
{
if
(!
phoneId
.
HasValue
)
return
RedirectToAction
(
"Error"
,
"Errors"
,
new
{
statusCode
=
777
}
);
if
(!
phoneId
.
HasValue
)
return
ValidationProblem
(
);
var
phoneViewModel
=
_phoneService
.
GetById
(
phoneId
.
Value
);
return
View
(
phoneViewModel
);
return
Ok
(
phoneViewModel
);
}
catch
(
NullReferenceException
)
{
return
RedirectToAction
(
"Error"
,
"Errors"
,
new
{
statusCode
=
777
}
);
return
BadRequest
(
);
}
}
}
}
\ No newline at end of file
PhoneStore/Models/Brand.cs
View file @
6e40b765
using
System.Text.Json.Serialization
;
namespace
PhoneStore.Models
{
public
class
Brand
{
[
JsonPropertyName
(
"id"
)]
public
int
Id
{
get
;
set
;
}
[
JsonPropertyName
(
"name"
)]
public
string
Name
{
get
;
set
;
}
}
}
\ No newline at end of file
PhoneStore/Startup.cs
View file @
6e40b765
...
...
@@ -39,11 +39,11 @@ namespace PhoneStore
if
(
env
.
IsDevelopment
())
{
app
.
UseDeveloperExceptionPage
();
app
.
UseStatusCodePagesWithRedirects
(
"/Error/{0}"
);
//
app.UseStatusCodePagesWithRedirects("/Error/{0}");
}
else
{
app
.
UseExceptionHandler
(
"/Home/Error"
);
//
app.UseExceptionHandler("/Home/Error");
app
.
UseHsts
();
}
...
...
PhoneStore/ViewModels/BaseEntity.cs
View file @
6e40b765
using
System.Text.Json.Serialization
;
namespace
PhoneStore.ViewModels
{
public
class
BaseEntity
{
[
JsonPropertyName
(
"id"
)]
public
int
Id
{
get
;
set
;
}
}
}
\ No newline at end of file
PhoneStore/ViewModels/PhoneViewModels/PhoneViewModel.cs
View file @
6e40b765
using
System.Collections.Generic
;
using
System.Text.Json.Serialization
;
using
PhoneStore.Models
;
using
PhoneStore.ViewModels.Feedback
;
...
...
@@ -6,11 +7,17 @@ namespace PhoneStore.ViewModels.PhoneViewModels
{
public
class
PhoneViewModel
:
BaseEntity
{
[
JsonPropertyName
(
"name"
)]
public
string
Name
{
get
;
set
;
}
[
JsonPropertyName
(
"price"
)]
public
decimal
Price
{
get
;
set
;
}
[
JsonPropertyName
(
"brandId"
)]
public
int
BrandId
{
get
;
set
;
}
[
JsonPropertyName
(
"brand"
)]
public
Brand
Brand
{
get
;
set
;
}
[
JsonPropertyName
(
"image"
)]
public
string
Image
{
get
;
set
;
}
[
JsonPropertyName
(
"feedbacks"
)]
public
IEnumerable
<
FeedbackViewModel
>
Feedbacks
{
get
;
set
;
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment