Частично покрыл тестами PhoneController.

parent a5a0c3ef
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 = 3000M,
Brand = new Brand{Name = "Adidas"}
};
context.Phones.Add(phone);
context.SaveChanges();
return phone.Id;
}
}
}
\ No newline at end of file
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneStore", "PhoneStore\PhoneStore.csproj", "{32DC0801-63D0-4261-9F07-8DD570B35335}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneStore", "PhoneStore\PhoneStore.csproj", "{32DC0801-63D0-4261-9F07-8DD570B35335}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneStore.Tests", "PhoneStore.Tests\PhoneStore.Tests.csproj", "{3DF8B5C0-BB98-4A2E-A589-5311ECBE546D}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
...@@ -12,5 +14,9 @@ Global ...@@ -12,5 +14,9 @@ Global
{32DC0801-63D0-4261-9F07-8DD570B35335}.Debug|Any CPU.Build.0 = Debug|Any CPU {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.ActiveCfg = Release|Any CPU
{32DC0801-63D0-4261-9F07-8DD570B35335}.Release|Any CPU.Build.0 = 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 EndGlobalSection
EndGlobal EndGlobal
...@@ -54,29 +54,53 @@ namespace PhoneStore.Controllers ...@@ -54,29 +54,53 @@ namespace PhoneStore.Controllers
return View(model); 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] [HttpPost]
public async Task<IActionResult> Create(PhoneCreateViewModel model) public async Task<IActionResult> Create([FromBody]PhoneCreateViewModel model)
{ {
try try
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
await _phoneService.CreateAsync(model); await _phoneService.CreateAsync(model);
return Ok();
return RedirectToAction("Index");
} }
model.Brands = _db.Brands.ToList(); model.Brands = _db.Brands.ToList();
return View("Create", model); return ValidationProblem();
} }
//TODO: Добавить кастомный exception
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
return RedirectToAction("Error", "Errors", new {statusCode = 666}); return BadRequest();
} }
catch(Exception) catch(Exception)
{ {
return RedirectToAction("Error", "Errors", new {statusCode = 777}); return BadRequest();
} }
} }
...@@ -138,19 +162,34 @@ namespace PhoneStore.Controllers ...@@ -138,19 +162,34 @@ namespace PhoneStore.Controllers
return RedirectToAction("Index"); 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] [HttpGet]
public IActionResult About(int? phoneId) public IActionResult About(int? phoneId)
{ {
try try
{ {
if (!phoneId.HasValue) return RedirectToAction("Error", "Errors", new {statusCode = 777}); if (!phoneId.HasValue) return ValidationProblem();
var phoneViewModel = _phoneService.GetById(phoneId.Value); var phoneViewModel = _phoneService.GetById(phoneId.Value);
return View(phoneViewModel); return Ok(phoneViewModel);
} }
catch (NullReferenceException) catch (NullReferenceException)
{ {
return RedirectToAction("Error", "Errors", new {statusCode = 777}); return BadRequest();
} }
} }
} }
} }
\ No newline at end of file
using System.Text.Json.Serialization;
namespace PhoneStore.Models namespace PhoneStore.Models
{ {
public class Brand public class Brand
{ {
[JsonPropertyName("id")]
public int Id { get; set; } public int Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; } public string Name { get; set; }
} }
} }
\ No newline at end of file
...@@ -39,11 +39,11 @@ namespace PhoneStore ...@@ -39,11 +39,11 @@ namespace PhoneStore
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
app.UseStatusCodePagesWithRedirects("/Error/{0}"); // app.UseStatusCodePagesWithRedirects("/Error/{0}");
} }
else else
{ {
app.UseExceptionHandler("/Home/Error"); // app.UseExceptionHandler("/Home/Error");
app.UseHsts(); app.UseHsts();
} }
......
using System.Text.Json.Serialization;
namespace PhoneStore.ViewModels namespace PhoneStore.ViewModels
{ {
public class BaseEntity public class BaseEntity
{ {
[JsonPropertyName("id")]
public int Id { get; set; } public int Id { get; set; }
} }
} }
\ No newline at end of file
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.Json.Serialization;
using PhoneStore.Models; using PhoneStore.Models;
using PhoneStore.ViewModels.Feedback; using PhoneStore.ViewModels.Feedback;
...@@ -6,11 +7,17 @@ namespace PhoneStore.ViewModels.PhoneViewModels ...@@ -6,11 +7,17 @@ namespace PhoneStore.ViewModels.PhoneViewModels
{ {
public class PhoneViewModel : BaseEntity public class PhoneViewModel : BaseEntity
{ {
[JsonPropertyName("name")]
public string Name { get; set; } public string Name { get; set; }
[JsonPropertyName("price")]
public decimal Price { get; set; } public decimal Price { get; set; }
[JsonPropertyName("brandId")]
public int BrandId { get; set; } public int BrandId { get; set; }
[JsonPropertyName("brand")]
public Brand Brand { get; set; } public Brand Brand { get; set; }
[JsonPropertyName("image")]
public string Image { get; set; } public string Image { get; set; }
[JsonPropertyName("feedbacks")]
public IEnumerable<FeedbackViewModel> Feedbacks { get; set; } public IEnumerable<FeedbackViewModel> Feedbacks { get; set; }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment