Skip to content

Commit 5cbd320

Browse files
committed
update project
1 parent d4c957c commit 5cbd320

File tree

15 files changed

+418
-109
lines changed

15 files changed

+418
-109
lines changed

WebEcomerce/Controllers/AdminController.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,30 @@
1111
using System.Net.Http.Headers;
1212
using System.Text;
1313
using System.IO;
14+
using System.Net.Http;
15+
using WebEcomerce.Services;
1416

1517
namespace WebEcomerce.Controllers
1618
{
1719
public class AdminController : Controller
1820
{
1921
private readonly IProductRepository productRepository;
2022
private Product modelProduct = new Product();
21-
private List<Product> products = new List<Product>();
23+
private readonly APIService _apiService;
2224
public AdminController(IProductRepository productRepository)
2325
{
26+
_apiService = new APIService();
2427
this.productRepository = productRepository;
25-
this.products = productRepository.GetAllProduct().ToList();
2628
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"D:\Work\Workshopecomerce\WebEcomerce\My First Project-fc65b82dffda.json");
2729
}
2830

2931
[HttpGet]
3032
public IActionResult ManageProduct()
3133
{
32-
if (this.products.Any())
33-
{
34-
return View(this.products.Where(it => it.Deleted_at == null).ToList());
35-
}
36-
else
37-
{
38-
return View();
39-
}
34+
var products = _apiService.GetProducts();
35+
return View(products);
4036
}
4137

42-
4338
public IActionResult Add()
4439
{
4540
return View();
@@ -48,25 +43,24 @@ public IActionResult Add()
4843
[HttpPost]
4944
public IActionResult Add(string ProductName, string ProductDetail, double Price, int Amount, IFormFile ImagePath)
5045
{
51-
var time = (int)(DateTime.UtcNow.ToLocalTime() - new DateTime(2020, 1, 1)).TotalSeconds;
52-
var objectName = $"test/{time.ToString()}.png";
5346
modelProduct.Id = Guid.NewGuid().ToString();
5447
modelProduct.ProductName = ProductName;
5548
modelProduct.ProductDetail = ProductDetail;
5649
modelProduct.Price = Price;
5750
modelProduct.Amount = Amount;
58-
modelProduct.ImagePath = UploadFile("storage-image-test", ImagePath, objectName);
51+
modelProduct.ImagePath = UploadFile("storage-image-test", ImagePath);
5952
modelProduct.Created_at = DateTime.Now;
60-
productRepository.Create(modelProduct);
61-
return RedirectToAction("ManageProduct");
53+
var response = _apiService.AddProducts(modelProduct);
54+
if (response)
55+
{
56+
return RedirectToAction("ManageProduct");
57+
}
58+
else
59+
{
60+
return RedirectToAction("Add");
61+
}
6262
}
6363

64-
//public string UploadImage()
65-
//{
66-
// var time = (int)(DateTime.UtcNow.ToLocalTime() - new DateTime(2020, 1, 1)).TotalSeconds;
67-
// var objectName = $"test/{time.ToString()}.png";
68-
//}
69-
7064
[HttpGet]
7165
public IActionResult Edit(string id)
7266
{
@@ -81,7 +75,7 @@ public IActionResult Edit(Product product)
8175
return RedirectToAction("ManageProduct");
8276
}
8377

84-
78+
[HttpPost]
8579
public IActionResult DeleteProduct(string id)
8680
{
8781
var product = productRepository.Get(it => it.Id == id);
@@ -95,8 +89,10 @@ public IActionResult Detail(string id)
9589
return View(productRepository.Get(it => it.Id == id));
9690
}
9791

98-
private string UploadFile(string bucketName, IFormFile localPath, string objectName = null)
92+
private string UploadFile(string bucketName, IFormFile localPath)
9993
{
94+
var time = (int)(DateTime.UtcNow.ToLocalTime() - new DateTime(2020, 1, 1)).TotalSeconds;
95+
var objectName = $"test/{time.ToString()}.png";
10096
var storage = StorageClient.Create();
10197
storage.UploadObject(bucketName, objectName, null, localPath.OpenReadStream());
10298
var storageObject = storage.GetObject(bucketName, objectName);

WebEcomerce/Controllers/HomeController.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,33 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Net.Http;
56
using System.Threading.Tasks;
67
using Demoecomerce.Reponsitories;
78
using Microsoft.AspNetCore.Mvc;
89
using WebEcomerce.Models;
10+
using WebEcomerce.Services;
911

1012
namespace WebEcomerce.Controllers
1113
{
1214
public class HomeController : Controller
1315
{
14-
private readonly IProductRepository productRepository;
15-
private Product modelProduct = new Product();
16+
private readonly APIService _apiService;
1617

17-
public HomeController(IProductRepository productRepository)
18+
public HomeController()
1819
{
19-
this.productRepository = productRepository;
20+
_apiService = new APIService();
2021
}
2122

22-
public List<Product> GetProducts()
23-
=> productRepository.GetAllProduct().Where(it => it.Deleted_at == null).ToList();
24-
25-
2623
public IActionResult Index()
2724
{
28-
return View(GetProducts());
25+
return View(_apiService.GetProducts());
26+
}
27+
28+
[HttpGet]
29+
public IActionResult ViewProduct(string id)
30+
{
31+
return View(_apiService.GetProduct(id));
2932
}
3033

3134
public IActionResult Privacy()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Demoecomerce.Reponsitories;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Mvc;
8+
using WebEcomerce.Models;
9+
10+
namespace WebEcomerce.Controllers.api
11+
{
12+
[Route("api/[controller]/[action]")]
13+
[ApiController]
14+
public class ProductController : ControllerBase
15+
{
16+
private IProductRepository _product;
17+
public ProductController(IProductRepository product)
18+
{
19+
_product = product;
20+
}
21+
22+
[HttpGet]
23+
public List<Product> GetProducts()
24+
=> _product.GetAllProduct().ToList();
25+
26+
[HttpGet]
27+
public Product GetProductById(string id)
28+
=> _product.Get(it => it.Id == id);
29+
30+
[HttpPost]
31+
public void AddProducts(Product product)
32+
=> _product.Create(product);
33+
}
34+
}

WebEcomerce/Models/Product.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Product
2323
public DateTime? Created_at { get; set; }
2424
public DateTime? Updated_at { get; set; }
2525
public DateTime? Deleted_at { get; set; }
26-
26+
2727
}
28+
2829
}

WebEcomerce/Services/APIService.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using WebEcomerce.Models;
9+
10+
namespace WebEcomerce.Services
11+
{
12+
public class APIService
13+
{
14+
private readonly HttpClientHandler _clientHandler = new HttpClientHandler();
15+
private List<Product> _products = new List<Product>();
16+
private Product _product = new Product();
17+
18+
19+
public APIService()
20+
{
21+
_clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
22+
}
23+
24+
public List<Product> GetProducts()
25+
{
26+
using (var client = new HttpClient(_clientHandler))
27+
{
28+
var resultTask = client.GetAsync("https://localhost:44374/api/Product/GetProducts");
29+
if (resultTask.Result.IsSuccessStatusCode)
30+
{
31+
var readTask = resultTask.Result.Content.ReadAsAsync<List<Product>>();
32+
readTask.Wait();
33+
_products = readTask.Result.Where(it => it.Deleted_at == null).ToList();
34+
}
35+
}
36+
return _products;
37+
}
38+
39+
public bool AddProducts(Product product)
40+
{
41+
bool result;
42+
var test = JsonConvert.SerializeObject(product);
43+
var content = new StringContent(test, Encoding.UTF8, "application/json");
44+
using (var client = new HttpClient(_clientHandler))
45+
{
46+
var postTask = client.PostAsync("https://localhost:44374/api/Product/AddProducts", content);
47+
postTask.Wait();
48+
result = postTask.Result.IsSuccessStatusCode;
49+
}
50+
return result;
51+
}
52+
53+
public Product GetProduct(string id)
54+
{
55+
using (var client = new HttpClient(_clientHandler))
56+
{
57+
var resultTask = client.GetAsync("https://localhost:44374/api/Product/GetProductById?id=" + id);
58+
if (resultTask.Result.IsSuccessStatusCode)
59+
{
60+
var readTask = resultTask.Result.Content.ReadAsAsync<Product>();
61+
readTask.Wait();
62+
_product = readTask.Result;
63+
}
64+
}
65+
return _product;
66+
}
67+
}
68+
}

WebEcomerce/Services/DbService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public DbService(DbConfig dbConfig)
1818

1919
CollectionProduct = database.GetCollection<Product>(dbConfig.Product);
2020
CollectionUser = database.GetCollection<User>(dbConfig.User);
21-
2221
}
23-
2422
}
2523
}

WebEcomerce/Views/Admin/Add.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@model WebEcomerce.Models.Product
22

33
@{
4-
ViewData["Title"] = "View";
4+
ViewData["Title"] = "Add Product";
55
Layout = "~/Views/Shared/_Layout.cshtml";
66
}
77

WebEcomerce/Views/Admin/ManageProduct.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<tr>
3333
<th scope="row">@i</th>
3434
<td>@product.ProductName</td>
35-
<td>@string.Format("{0:N}", @product.Price)</td>
35+
<td>฿@string.Format("{0:N}", @product.Price)</td>
3636
<td>@product.Amount</td>
3737
<td class="d-flex flex-wrap justify-content-around align-items-md-center">
3838
<button class="btn btn-info" onclick="location.href='@Url.Action("Detail", "Admin", new { id = @product.Id })'">View</button>

WebEcomerce/Views/Home/Index.cshtml

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,89 @@
33
}
44

55
<style>
6-
body{
6+
body {
77
background-color: #383838;
88
}
9-
.display-4{
9+
10+
.display-4,
11+
.display-descripton {
1012
color: white;
1113
text-align: center;
14+
font-weight: 700;
15+
}
16+
17+
.display-descripton {
18+
font-weight: 300;
19+
font-size: 22px;
20+
}
21+
22+
.font-title {
23+
padding: 25px 0;
24+
}
25+
26+
.img-box {
27+
max-width: 290px;
28+
overflow: hidden;
29+
position: relative;
30+
}
31+
32+
.overlay {
33+
position: absolute;
34+
top: 0;
35+
left: 0;
36+
width: 100%;
37+
height: 100%;
38+
background: rgba(0, 0, 0, 0);
39+
transition: background 0.5s ease;
40+
}
41+
42+
.img-box:hover .overlay {
43+
display: block;
44+
background: rgba(0, 0, 0, .7);
45+
}
46+
47+
.img-box img {
48+
width: 100%;
49+
transition: transform .5s ease;
1250
}
51+
52+
.btn-view {
53+
position: absolute;
54+
top: 50%;
55+
left: 50%;
56+
transform: translate(-50%, -50%);
57+
padding: 12px 48px;
58+
opacity: 0;
59+
transition: opacity .35s ease;
60+
text-align: center;
61+
color: white;
62+
border: solid 2px white;
63+
z-index: 1;
64+
cursor: pointer;
65+
font-weight:500;
66+
font-size: 18px;
67+
}
68+
69+
.img-box:hover .btn-view {
70+
opacity: 1;
71+
}
72+
73+
1374
</style>
1475

15-
<div>
76+
<div class="font-title">
1677
<h3 class="display-4">Keycaps</h3>
78+
<p class="display-descripton">Engineered for the ultimate typing experience</p>
1779
</div>
1880
<div class="row">
19-
@* @foreach (var product in Model)
20-
{
21-
<div class="col-md-3">
22-
<div class="card h-100">
23-
<img class="card-img-top" src="@product.ImagePath" alt="Card image cap">
24-
<div class="card-body">
25-
<h5 class="card-title">@product.ProductName</h5>
26-
<p class="card-text"><small class="text-muted">฿@string.Format("{0:N}", @product.Price)</small></p>
27-
</div>
28-
<div class="card-body">
29-
<a href="#" class="btn btn-primary">Go somewhere</a>
30-
</div>
31-
</div>
32-
</div>
33-
} *@
3481
@foreach (var product in Model)
3582
{
3683
<div class="col-md-4">
3784
<div class="product">
3885
<div class="img-box">
3986
<img src="@product.ImagePath">
87+
<div class="overlay"></div>
88+
<div class="btn-view" onclick="location.href='@Url.Action("ViewProduct", "Home", new { id = @product.Id })'"> View </div>
4089
</div>
4190
<p class="title">@product.ProductName</p>
4291
<p class="price">฿@string.Format("{0:N}", @product.Price)</p>

0 commit comments

Comments
 (0)