Skip to content

Commit

Permalink
Day_03_Lab_06
Browse files Browse the repository at this point in the history
  • Loading branch information
PiAlexay committed Jun 8, 2022
1 parent 9914ab3 commit ca63eab
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ButterfliesShop.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System.IO;

namespace ButterfliesShop.Controllers
{
Expand Down Expand Up @@ -35,12 +36,74 @@ private void InitializeButterfliesData()
}
}

public IActionResult Index()
{
IndexViewModel indexViewModel = new IndexViewModel();
indexViewModel.Butterflies = _data.ButterfliesList;
return View(indexViewModel);

}
[HttpGet]
public IActionResult Create()
{
return View();
}

[HttpPost]
public IActionResult Create(Butterfly butterfly)
{
if (ModelState.IsValid)
{
Butterfly lastButterfly = _data.ButterfliesList.LastOrDefault();
butterfly.CreatedDate = DateTime.Today;
if (butterfly.PhotoAvatar != null && butterfly.PhotoAvatar.Length > 0)
{
butterfly.ImageMimeType = butterfly.PhotoAvatar.ContentType;
butterfly.ImageName = Path.GetFileName(butterfly.PhotoAvatar.FileName);
butterfly.Id = lastButterfly.Id + 1;
_butterfliesQuantityService.AddButterfliesQuantityData(butterfly);
using (var memoryStream = new MemoryStream())
{
butterfly.PhotoAvatar.CopyTo(memoryStream);
butterfly.PhotoFile = memoryStream.ToArray();
}
_data.AddButterfly(butterfly);
return RedirectToAction("Index");
}
return View(butterfly);
}
return View(butterfly);
}

public IActionResult GetImage(int id)
{
Butterfly requestedButterfly = _data.GetButterflyById(id);
if (requestedButterfly != null)
{
return null;
string webRootpath = _environment.WebRootPath;
string folderPath = "\\images\\";
string fullPath = webRootpath + folderPath + requestedButterfly.ImageName;
if (System.IO.File.Exists(fullPath))
{
FileStream fileOnDisk = new FileStream(fullPath, FileMode.Open);
byte[] fileBytes;
using (BinaryReader br = new BinaryReader(fileOnDisk))
{
fileBytes = br.ReadBytes((int)fileOnDisk.Length);
}
return File(fileBytes, requestedButterfly.ImageMimeType);
}
else
{
if (requestedButterfly.PhotoFile.Length > 0)
{
return File(requestedButterfly.PhotoFile, requestedButterfly.ImageMimeType);
}
else
{
return NotFound();
}
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,45 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
using ButterfliesShop.Validators;

namespace ButterfliesShop.Models
{
public class Butterfly
{
public int Id { get; set; }

[Display(Name = "Common Name:")]
[Required(ErrorMessage = "Please enter the butterfly name")]
public string CommonName { get; set; }

[Display(Name = "Butterfly Family:")]
[Required(ErrorMessage = "Please select the butterfly family")]
public Family? ButterflyFamily { get; set; }

[Display(Name = "Butterflies Quantity:")]
[Required(ErrorMessage = "Please select the butterfly quantity")]
[MaxButterflyQuantityValidation(50)]
public int? Quantity { get; set; }

[Display(Name = "Characteristics:")]
[Required(ErrorMessage = "Please type the characteristics")]
[StringLength(50)]
public string Characteristics { get; set; }

[DataType(DataType.DateTime)]
[Display(Name = "Updated on:")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yy}")]
public DateTime CreatedDate { get; set; }

[Display(Name = "Butterflies Picture:")]
[Required(ErrorMessage = "Please select the butterflies picture")]
public IFormFile PhotoAvatar { get; set; }
public string ImageName { get; set; }
public byte[] PhotoFile { get; set; }
public string ImageMimeType { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace ButterfliesShop.Models
{
public class IndexViewModel
{
public List<Butterfly> Butterflies { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Threading.Tasks;
using ButterfliesShop.Models;
using ButterfliesShop.Services;
using System.ComponentModel.DataAnnotations;

namespace ButterfliesShop.Validators
{
public class MaxButterflyQuantityValidation : ValidationAttribute
{
private int _maxAmount;
public MaxButterflyQuantityValidation(int maxAmount)
{
_maxAmount = maxAmount;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{

var service = (IButterfliesQuantityService)validationContext.GetService(typeof(IButterfliesQuantityService));
Butterfly butterfly = (Butterfly)validationContext.ObjectInstance;
if (butterfly.ButterflyFamily != null)
{
int? quantity = service.GetButterflyFamilyQuantity(butterfly.ButterflyFamily.Value);
int? sumQuantity = quantity + butterfly.Quantity;
if (sumQuantity > _maxAmount)
{
return new ValidationResult(string.Format("Limit of butterflies from the same family in the store is {0} butterflies. Currently there are {1}", _maxAmount, quantity));
}
return ValidationResult.Success;
}
return ValidationResult.Success;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

@using ButterfliesShop.Models
@model ButterfliesShop.Models.Butterfly
@{
Layout = null;
}
Expand All @@ -12,5 +13,41 @@
<link type="text/css" rel="stylesheet" href="~/css/butterflies-shop-styles.css" />
</head>
<body>
<div class="container">
<h1 class="main-title">Add Butterflies to the Shop</h1>
<form method="post" enctype="multipart/form-data" asp-action="Create">
<div asp-validation-summary="All"></div>
<div class="form-field">
<label asp-for="CommonName"></label>
<input asp-for="CommonName" />
<span asp-validation-for="CommonName"></span>
</div>
<div class="form-field">
<label asp-for="ButterflyFamily"></label>
<select asp-for="ButterflyFamily" asp-items="Html.GetEnumSelectList<Family>()">
<option selected="selected" value="">Select</option>
</select>
<span asp-validation-for="ButterflyFamily"></span>
</div>
<div class="form-field">
<label asp-for="Characteristics"></label>
<textarea asp-for="Characteristics"></textarea>
<span asp-validation-for="Characteristics"></span>
</div>
<div class="form-field">
<label asp-for="Quantity"></label>
<input asp-for="Quantity" />
<span asp-validation-for="Quantity"></span>
</div>
<div class="form-field">
<label asp-for="PhotoAvatar"></label>
<input asp-for="PhotoAvatar" type="file" />
<span asp-validation-for="PhotoAvatar"></span>
</div>
<div class="form-field">
<input type="submit" value="Submit" />
</div>
</form>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

@model ButterfliesShop.Models.IndexViewModel
@{
Layout = null;
}
Expand All @@ -12,5 +12,60 @@
<link type="text/css" rel="stylesheet" href="~/css/butterflies-shop-styles.css" />
</head>
<body>
<div class="container">
<h1 class="main-title">Butterflies Shop</h1>
<p class="into">Welcome to our Web Store, Enjoy a Wide Variety of Butterflies</p>
<p class="into">Our Butterflies in the Shop</p>
<button type="button" onclick="location.href='@Url.Action("Create", "Butterfly")'">Add Butterflies</button>
<div class="img-container">
@foreach (var item in Model.Butterflies)
{
<div class="photo-index-card">
<h3 class="display-picture-title">
"@Html.DisplayFor(modelItem => item.CommonName)"
</h3>
@if (item.ImageName != null)
{
<div class="photo-display">
<img class="photo-display-img" src="@Url.Action("GetImage", "Butterfly", new { Id = item.Id })" />
</div>
}
<div>
<p class="display-label">
@Html.DisplayNameFor(model => item.ButterflyFamily)
</p>
<br />
<p class="display-field">
@Html.DisplayFor(model => item.ButterflyFamily)
</p>
</div>
<div class="display-info">
<p class="display-label">
@Html.DisplayNameFor(model => item.Characteristics)
</p>
<p class="display-field">
@Html.DisplayFor(model => item.Characteristics)
</p>
</div>
<div>
<p class="display-label">
@Html.DisplayNameFor(model => item.Quantity)
</p>
<p class="display-field">
@Html.DisplayFor(model => item.Quantity)
</p>
</div>
<div>
<p class="display-label">
@Html.DisplayNameFor(model => item.CreatedDate)
</p>
<p class="display-field">
@Html.DisplayFor(model => item.CreatedDate)
</p>
</div>
</div>
}
</div>
</div>
</body>
</html>

0 comments on commit ca63eab

Please sign in to comment.