Skip to content
Prev Previous commit
Next Next commit
added admin panel
  • Loading branch information
Sedat Girgin committed Feb 20, 2021
commit e4a4f91fdceaff4e5e0025273b65d8e8c31dfef9
39 changes: 38 additions & 1 deletion src/WebApp/AspnetRunBasics/ApiCollection/CatalogApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AspnetRunBasics.ApiCollection.Interfaces;
using AspnetRunBasics.Models;
using AspnetRunBasics.Settings;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
Expand Down Expand Up @@ -51,12 +52,23 @@ public async Task<IEnumerable<CatalogModel>> GetCatalogByCategory(string categor
.GetHttpMessage();

return await SendRequest<IEnumerable<CatalogModel>>(message);
}

public async Task<CatalogModel> GetProduct(string id)
{
var message = new HttpRequestBuilder(_settings.BaseAddress)
.SetPath(_settings.CatalogPath)
.AddToPath(id)
.HttpMethod(HttpMethod.Get)
.GetHttpMessage();

return await SendRequest<CatalogModel>(message);
}

public async Task<CatalogModel> CreateCatalog(CatalogModel catalogModel)
{
var message = new HttpRequestBuilder(_settings.BaseAddress)
.SetPath(_settings.CatalogPath)
.SetPath(_settings.CatalogPath)
.HttpMethod(HttpMethod.Post)
.GetHttpMessage();

Expand All @@ -65,5 +77,30 @@ public async Task<CatalogModel> CreateCatalog(CatalogModel catalogModel)

return await SendRequest<CatalogModel>(message);
}

public async Task<IActionResult> UpdateCatalog(CatalogModel catalogModel)
{
var message = new HttpRequestBuilder(_settings.BaseAddress)
.SetPath(_settings.CatalogPath)
.HttpMethod(HttpMethod.Put)
.GetHttpMessage();

var json = JsonConvert.SerializeObject(catalogModel);
message.Content = new StringContent(json, Encoding.UTF8, "application/json");

return await SendRequest<IActionResult>(message);
}

public async Task<IActionResult> DeleteCatalog(string id)
{
var message = new HttpRequestBuilder(_settings.BaseAddress)
.SetPath(_settings.CatalogPath)
.HttpMethod(HttpMethod.Delete)
.AddToPath(id)
.GetHttpMessage();

return await SendRequest<IActionResult>(message);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AspnetRunBasics.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand All @@ -10,5 +11,9 @@ public interface ICatalogApi
Task<IEnumerable<CatalogModel>> GetCatalogByCategory(string category);
Task<CatalogModel> GetCatalog(string id);
Task<CatalogModel> CreateCatalog(CatalogModel model);
Task<CatalogModel> GetProduct(string id);
Task<IActionResult> UpdateCatalog(CatalogModel catalogModel);
Task<IActionResult> DeleteCatalog(string id);

}
}
123 changes: 123 additions & 0 deletions src/WebApp/AspnetRunBasics/Areas/Admin/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using AspnetRunBasics.ApiCollection.Interfaces;
using AspnetRunBasics.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace AspnetRunBasics.Areas.Admin.Controllers
{
public class HomeController : Controller
{
private readonly ICatalogApi _catalogApi;

public HomeController(ICatalogApi catalogApi, IBasketApi basketApi)
{
_catalogApi = catalogApi ?? throw new ArgumentNullException(nameof(catalogApi));
}

public async Task<IActionResult> IndexAsync()
{
var result = await _catalogApi.GetCatalog();

return View(result);
}
public IActionResult Add()
{
return View(new AddCatalogModel());
}

[HttpPost]
public IActionResult Add(AddCatalogModel catalog)
{
if (ModelState.IsValid)
{
CatalogModel catalogModel = new CatalogModel();

if (catalog.ImageURL != null)
{
var type = Path.GetExtension(catalog.ImageURL.FileName);
var newImageName = Guid.NewGuid() + type;

var NewImagePath = Path.Combine(Directory.GetCurrentDirectory(),
"wwwroot/images/product/" + newImageName);
var stream = new FileStream(NewImagePath, FileMode.Create);

//resmi NewImagePath adresine kopyaladık
catalog.ImageURL.CopyTo(stream);
catalogModel.ImageFile = newImageName;
}

catalogModel.Name = catalog.Name;
catalogModel.Description = catalog.Description;
catalogModel.Price = catalog.Price;
catalogModel.Category = catalog.Category;
catalogModel.Summary = catalog.Summary;

_catalogApi.CreateCatalog(catalogModel);
return RedirectToAction("Index", "Home", new { area = "Admin" });
}
return View(catalog);
}


public async Task<IActionResult> UpdateAsync(string id)
{
var catalogModel = await _catalogApi.GetProduct(id);


AddCatalogModel addCatalogModel = new AddCatalogModel
{
Id = catalogModel.Id,
Name = catalogModel.Name,
Description = catalogModel.Description,
Price = catalogModel.Price,
Category = catalogModel.Category,
Summary = catalogModel.Summary
};
return View(addCatalogModel);
}

[HttpPost]
public async Task<IActionResult> UpdateAsync(AddCatalogModel addCatalogModel)
{
if (ModelState.IsValid)
{
var catalogModel = await _catalogApi.GetProduct(addCatalogModel.Id);
if (addCatalogModel.ImageURL != null)
{
var type = Path.GetExtension(addCatalogModel.ImageURL.FileName);
var newImageName = Guid.NewGuid() + type;

var NewImagePath = Path.Combine(Directory.GetCurrentDirectory(),
"wwwroot/images/product/" + newImageName);
var stream = new FileStream(NewImagePath, FileMode.Create);

addCatalogModel.ImageURL.CopyTo(stream);
catalogModel.ImageFile = newImageName;

}
catalogModel.Id = addCatalogModel.Id;
catalogModel.Name = addCatalogModel.Name;
catalogModel.Description = addCatalogModel.Description;
catalogModel.Price = addCatalogModel.Price;
catalogModel.Category = addCatalogModel.Category;
catalogModel.Summary = addCatalogModel.Summary;

_catalogApi.UpdateCatalog(catalogModel);
return RedirectToAction("Index", "Home", new { area = "Admin" });
}
return View(addCatalogModel);
}

public IActionResult Delete(string id)
{
_catalogApi.DeleteCatalog(id);

return RedirectToAction("Index", "Home", new { area = "Admin" });
}

}
}
48 changes: 48 additions & 0 deletions src/WebApp/AspnetRunBasics/Areas/Admin/Views/Home/Add.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@model AddCatalogModel
@*enctype="multipart/form-data" form aracılıgı ile dosya taşınmasını sağlar 38. video*@
<form asp-action="Add" asp-controller="Home" asp-area="Admin" enctype="multipart/form-data" method="post" class="p-4 w-50 mx-auto shadow-lg">

<div class="form-group">
<label class="col-form-label"> Name</label>
<span asp-validation-for="@Model.Name" class="text-danger"></span>
<input type="text" asp-for="@Model.Name" class="form-control" />
</div>

<div class="form-group">
<label class="col-form-label"> Category</label>
<span asp-validation-for="@Model.Category" class="text-danger"></span>
<input type="text" asp-for="@Model.Category" class="form-control" />
</div>

<div class="form-group">
<label class="col-form-label"> Summary</label>
<span asp-validation-for="@Model.Summary" class="text-danger"></span>
<input type="text" asp-for="@Model.Summary" class="form-control" />
</div>

<div class="form-group">
<label class="col-form-label"> Price </label>
<span asp-validation-for="@Model.Price" class="text-danger"></span>
<input type="text" asp-for="@Model.Price" class="form-control" />
</div>
<div class="form-group">
<label class="col-form-label"> Description </label>
<input type="text" asp-for="@Model.Description" class="form-control" />
</div>

<div class="form-group">
<label class="col-form-label"> ImageURL </label>
<input type="file" asp-for="@Model.ImageURL" class="form-control-file" />
</div>

<div class="form-group">
<a type="submit" class="btn btn-primary" asp-action="Index" asp-controller="Home" asp-area="Admin"> Back </a>
<button type="submit" class="btn btn-primary"> Save </button>
</div>

</form>

@section ValidationClientControl{
<script src="~/jquery-validate/jquery.validate.min.js"></script>
<script src="~/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}
35 changes: 35 additions & 0 deletions src/WebApp/AspnetRunBasics/Areas/Admin/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@model List<CatalogModel>

<a asp-action="Add" asp-controller="Home" asp-area="Admin" class="btn btn-primary btn-sm my-2">Add </a>

<table class="table table-sm table-bordered table-hover mt-2">
<tr>
<td>Id</td>
<td>Name</td>
<td>Category</td>
<td>Summary</td>
<td>Description</td>
<td>ImageFile</td>
<td>Price</td>
</tr>
@foreach (var item in Model)
{

<tr>
<td>@item.Id</td>
<td>@item.Name</td>
<td>@item.Category</td>
<td>@item.Summary</td>
<td>@item.Description</td>
<td>@item.ImageFile</td>
<td>@item.Price</td>

<td>
<a asp-action="Update" asp-controller="Home" asp-area="Admin" asp-route-id="@item.Id" class="btn btn-warning btn-sm">Update</a>
<a asp-action="Delete" asp-controller="Home" asp-area="Admin" asp-route-id="@item.Id" class="btn btn-danger btn-sm">Delete</a>
</td>

</tr>
}
</table>

52 changes: 52 additions & 0 deletions src/WebApp/AspnetRunBasics/Areas/Admin/Views/Home/Update.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@model AddCatalogModel

<form asp-action="Update" asp-controller="Home" asp-area="Admin" enctype="multipart/form-data" method="post" class="p-4 w-50 mx-auto shadow-lg">

<input type="hidden" asp-for="@Model.Id" class="form-control" />


<div class="form-group">
<label class="col-form-label"> Name</label>
<span asp-validation-for="@Model.Name" class="text-danger"></span>
<input type="text" asp-for="@Model.Name" class="form-control" />
</div>

<div class="form-group">
<label class="col-form-label"> Category</label>
<span asp-validation-for="@Model.Category" class="text-danger"></span>
<input type="text" asp-for="@Model.Category" class="form-control" />
</div>

<div class="form-group">
<label class="col-form-label"> Summary</label>
<span asp-validation-for="@Model.Summary" class="text-danger"></span>
<input type="text" asp-for="@Model.Summary" class="form-control" />
</div>

<div class="form-group">
<label class="col-form-label"> Price </label>
<span asp-validation-for="@Model.Price" class="text-danger"></span>
<input type="text" asp-for="@Model.Price" class="form-control" />
</div>
<div class="form-group">
<label class="col-form-label"> Description </label>
<input type="text" asp-for="@Model.Description" class="form-control" />
</div>

<div class="form-group">
<label class="col-form-label"> ImageURL </label>
<input type="file" asp-for="@Model.ImageURL" class="form-control-file" />
</div>

<div class="form-group">
<a type="submit" class="btn btn-primary" asp-action="Index" asp-controller="Home" asp-area="Admin"> Back </a>
<button type="submit" class="btn btn-warning"> Update </button>
</div>

</form>


@section ValidationClientControl{
<script src="~/jquery-validate/jquery.validate.min.js"></script>
<script src="~/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}
39 changes: 39 additions & 0 deletions src/WebApp/AspnetRunBasics/Areas/Admin/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/twitter-bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
@*Navbar*@
<nav class="navbar navbar-expand-lg navbar-dark bg-danger">
<div class="container">
<a class="navbar-brand" href="#">Admin Panel</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" asp-action="Index" asp-controller="Home" asp-area="Admin">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Out <span class="sr-only">(current)</span></a>
</li>
</ul>
</div>
</div>
</nav>
@*Navbar*@

<div class="container-fluid">
@RenderBody()
</div>
<script src="~/twitter-bootstrap/js/bootstrap.min.js"></script>
<script src="~/jquery/jquery.min.js"></script>
@RenderSection("ValidationClientControl", false)

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@using AspnetRunBasics
@using AspnetRunBasics.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, AspnetRunBasics
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}
Loading