Skip to content

Commit d6e140a

Browse files
committed
Add PictureFileName seed and add rename PictureUri column name to PictureFileName
1 parent 4fe02d9 commit d6e140a

3 files changed

Lines changed: 44 additions & 31 deletions

File tree

src/Services/Catalog/Catalog.API/Controllers/PicController.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,32 @@ public PicController(IHostingEnvironment env,
2222
_catalogContext = catalogContext;
2323
}
2424

25-
[HttpGet("{filename}")]
25+
[HttpGet("{id}")]
2626
// GET: /<controller>/
27-
public IActionResult GetImage(string filename)
27+
public async Task<IActionResult> GetImage(int id)
2828
{
29-
var webRoot = _env.WebRootPath;
30-
var path = Path.Combine(webRoot, filename);
29+
if (id <= 0)
30+
{
31+
return BadRequest();
32+
}
3133

32-
string imageFileExtension = Path.GetExtension(filename);
33-
string mimetype = GetImageMimeTypeFromImageFileExtension(imageFileExtension);
34+
var item = await _catalogContext.CatalogItems
35+
.SingleOrDefaultAsync(ci => ci.Id == id);
36+
37+
if (item != null)
38+
{
39+
var webRoot = _env.WebRootPath;
40+
var path = Path.Combine(webRoot, item.PictureFileName);
3441

35-
var buffer = System.IO.File.ReadAllBytes(path);
42+
string imageFileExtension = Path.GetExtension(item.PictureFileName);
43+
string mimetype = GetImageMimeTypeFromImageFileExtension(imageFileExtension);
44+
45+
var buffer = System.IO.File.ReadAllBytes(path);
46+
47+
return File(buffer, mimetype);
48+
}
3649

37-
return File(buffer, mimetype);
50+
return NotFound();
3851
}
3952

4053
private string GetImageMimeTypeFromImageFileExtension(string extension)
@@ -43,29 +56,29 @@ private string GetImageMimeTypeFromImageFileExtension(string extension)
4356

4457
switch (extension)
4558
{
46-
case "png":
59+
case ".png":
4760
mimetype = "image/png";
4861
break;
49-
case "gif":
62+
case ".gif":
5063
mimetype = "image/gif";
5164
break;
52-
case "jpg":
53-
case "jpeg":
65+
case ".jpg":
66+
case ".jpeg":
5467
mimetype = "image/jpeg";
5568
break;
56-
case "bmp":
69+
case ".bmp":
5770
mimetype = "image/bmp";
5871
break;
59-
case "tiff":
72+
case ".tiff":
6073
mimetype = "image/tiff";
6174
break;
62-
case "wmf":
75+
case ".wmf":
6376
mimetype = "image/wmf";
6477
break;
65-
case "jp2":
78+
case ".jp2":
6679
mimetype = "image/jp2";
6780
break;
68-
case "svg":
81+
case ".svg":
6982
mimetype = "image/svg+xml";
7083
break;
7184
default:

src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static IEnumerable<CatalogItem> GetCatalogItemsFromFile(string contentRootPath,
189189
string[] csvheaders;
190190
try
191191
{
192-
string[] requiredHeaders = { "catalogtypename", "catalogbrandname", "description", "name", "price", "pictureuri" };
192+
string[] requiredHeaders = { "catalogtypename", "catalogbrandname", "description", "name", "price", "pictureFileName" };
193193
string[] optionalheaders = { "availablestock", "restockthreshold", "maxstockthreshold", "onreorder" };
194194
csvheaders = GetHeaders(csvFileCatalogItems, requiredHeaders, optionalheaders );
195195
}
@@ -320,18 +320,18 @@ static IEnumerable<CatalogItem> GetPreconfiguredItems()
320320
{
321321
return new List<CatalogItem>()
322322
{
323-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureUri = "1.png", AvailableStock = 100 },
324-
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "2.png", AvailableStock = 100 },
325-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "3.png", AvailableStock = 100 },
326-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureUri = "4.png", AvailableStock = 100 },
327-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "5.png", AvailableStock = 100 },
328-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureUri = "6.png", AvailableStock = 100 },
329-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "7.png", AvailableStock = 100 },
330-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureUri = "8.png", AvailableStock = 100 },
331-
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "9.png", AvailableStock = 100 },
332-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "10.png", AvailableStock = 100 },
333-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "11.png", AvailableStock = 100 },
334-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "12.png", AvailableStock = 100 }
323+
new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureFileName = "1.png" },
324+
new CatalogItem { CatalogTypeId = 1, CatalogBrandId = 2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureFileName = "2.png" },
325+
new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureFileName = "3.png" },
326+
new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureFileName = "4.png" },
327+
new CatalogItem { CatalogTypeId = 3, CatalogBrandId = 5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureFileName = "5.png" },
328+
new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureFileName = "6.png" },
329+
new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureFileName = "7.png" },
330+
new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureFileName = "8.png" },
331+
new CatalogItem { CatalogTypeId = 1, CatalogBrandId = 5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureFileName = "9.png" },
332+
new CatalogItem { CatalogTypeId = 3, CatalogBrandId = 2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureFileName = "10.png" },
333+
new CatalogItem { CatalogTypeId = 3, CatalogBrandId = 2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureFileName = "11.png" },
334+
new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureFileName = "12.png" },
335335
};
336336
}
337337

src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CatalogTypeName,CatalogBrandName,Description,Name,Price,PictureUri,availablestock,onreorder
1+
CatalogTypeName,CatalogBrandName,Description,Name,Price,PictureFileName,availablestock,onreorder
22
T-Shirt,.NET,".NET Bot Black Hoodie, and more",.NET Bot Black Hoodie,19.5,1.png,100,false
33
Mug,.NET,.NET Black & White Mug,.NET Black & White Mug,8.50,2.png,89,true
44
T-Shirt,Other,Prism White T-Shirt,Prism White T-Shirt,12,3.png,56,false

0 commit comments

Comments
 (0)