Skip to content

Commit 8f05e35

Browse files
author
Mitchell Marsh
committed
Finished getting database stuff
1 parent ce0b965 commit 8f05e35

File tree

10 files changed

+210
-12
lines changed

10 files changed

+210
-12
lines changed

3DPrintingBlockchainMarket/Controllers/AddModelController.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,23 @@ public AddModelController(IObjectModelService iobs,
3535
/// </summary>
3636
/// <param name="files">Filename must be the unique returned ID of the addition confirmaiton</param>
3737
/// <returns></returns>
38-
public async Task<JsonResult> UploadModelsAsync(List<IFormFile> files)
38+
[HttpPost]
39+
public async Task<JsonResult> UploadModelsAsync()
3940
{
40-
41+
var files = HttpContext.Request.Form.Files;
4142
long size = files.Sum(f => f.Length);
4243
// full path to file in temp location
43-
var filePath = Path.GetTempFileName();
44-
44+
Dictionary<string,string> TempFiles = new Dictionary<string, string>();
45+
46+
4547
foreach (var formFile in files)
4648
{
49+
var filePath = Path.GetTempFileName();
50+
TempFiles.Add(filePath, formFile.FileName);
51+
4752
if (formFile.Length > 0)
4853
{
49-
using (var stream = new FileStream(filePath, FileMode.Create))
54+
using (var stream = new FileStream(filePath, FileMode.OpenOrCreate))
5055
{
5156
await formFile.CopyToAsync(stream);
5257
}
@@ -56,10 +61,11 @@ public async Task<JsonResult> UploadModelsAsync(List<IFormFile> files)
5661
List<string> FailedFiles = new List<string>();
5762
//List of all added objects
5863
List<string> AddedModelPics = new List<string>();
59-
foreach (var file in Directory.GetFiles(filePath))
64+
65+
foreach (var file in TempFiles.Keys)
6066
{
6167
//Remove the filepath
62-
string FileNameWithExt = file.Remove(0, file.LastIndexOf('/'));
68+
string FileNameWithExt = TempFiles[file];
6369
//remove the file extension
6470
string FileName = FileNameWithExt.Remove(FileNameWithExt.Length - 4);
6571
using (var stream = new FileStream(file, FileMode.Open))
@@ -79,7 +85,7 @@ public async Task<JsonResult> UploadModelsAsync(List<IFormFile> files)
7985
case FileUploadType.JPG:
8086
{
8187
string pic = AddPictureRepresentation(stream, FileName);
82-
if(String.IsNullOrEmpty(pic)){ AddedModelPics.Add(pic); } else{ FailedFiles.Add(FileName); }
88+
if(!String.IsNullOrEmpty(pic)){ AddedModelPics.Add(pic); } else{ FailedFiles.Add(FileName); }
8389
break;
8490
}
8591
default:
@@ -138,7 +144,7 @@ private bool Add3DModelToFile(Stream stream, string FileName)
138144
{
139145

140146
//Add object to the database
141-
ObjectModel obj = _ObjectModelService.Get(FileName);
147+
ObjectModel obj = _ObjectModelService.Get(Guid.Parse(FileName));
142148
if (obj == null) return false;
143149
else
144150
{ // The file is a valid object.. verify the proper user is uploading the file
@@ -198,7 +204,7 @@ private string AddPictureRepresentation(Stream stream, string FileName)
198204
System.IO.File.WriteAllBytes(FilePath, inMem.ToArray());
199205
return FilePath;
200206
}
201-
public JsonResult ConfirmValidModel(UploadModelJson model)
207+
public JsonResult ConfirmValidModel([FromBody]UploadModelJson model)
202208
{
203209
if(String.IsNullOrEmpty(model.model_license_id)||
204210
String.IsNullOrEmpty(model.name) ||
@@ -234,6 +240,7 @@ public JsonResult ConfirmValidModel(UploadModelJson model)
234240
public FileUploadType FindFileType(Stream fileStream, string FileNameWithExtension)
235241
{
236242
string ext = FileNameWithExtension.Remove(0, FileNameWithExtension.Length - 3).ToUpper();
243+
//return FileUploadType.STL; // Temporary
237244
switch(ext)
238245
{
239246
case "PNG":

3DPrintingBlockchainMarket/Controllers/HomeController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ public HomeController(IEmailSender es)
2020
}
2121
public JsonResult Index()
2222
{
23-
//_EmailSender.SendModelConfirmationAsync("mitchell@marshhome.net");
24-
return Json(new { result = "yes"});
23+
UploadModelJson model = new UploadModelJson()
24+
{ description = "Container", model_license_id = "40C03B2D-370D-45B9-9BCC-015DB1FF90B6", name = "Bottle Container", pricing_unit_of_measure_id = "USD", tags = new List<string>() { "Bottle","Container","4 Pack","Pack","Beer","Holder" }, token_price = .10m };
25+
26+
return Json(model);
2527
}
2628

2729
public JsonResult About()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using _3DPrintingBlockchainMarket.Models;
2+
using _3DPrintingBlockchainMarket.Models.Json;
3+
using _3DPrintingBlockchainMarket.Services;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Net.Http;
11+
using System.Threading.Tasks;
12+
13+
namespace _3DPrintingBlockchainMarket.Controllers
14+
{
15+
public class ModelInfoController : Controller
16+
{
17+
private ILicenseService _LicService { get; }
18+
private IObjectModelService _ObjectModelService { get; }
19+
20+
public ModelInfoController(IObjectModelService ioms, ILicenseService ils)
21+
{
22+
_LicService = ils;
23+
_ObjectModelService = ioms;
24+
}
25+
26+
//Get All Licenses
27+
public JsonResult GetAllLicenses()
28+
{
29+
List<ModelLicenseJson> result = new List<ModelLicenseJson>();
30+
foreach(var ml in _LicService.GetAll<ModelLicense>())
31+
{
32+
result.Add(new ModelLicenseJson(ml));
33+
}
34+
return Json(result);
35+
}
36+
/// <summary>
37+
/// Return the top 20 of the result list for tags
38+
/// </summary>
39+
/// <param name="tags"></param>
40+
/// <returns></returns>
41+
public JsonResult SearchItems(params string[] tags)
42+
{
43+
//get All by browse search request
44+
List<ModelObjectJson> result = new List<ModelObjectJson>();
45+
foreach(var res in _ObjectModelService.Search(tags))
46+
{
47+
//return the picture link as well
48+
result.Add(new ModelObjectJson(res, Url.Content(Path.Combine("wwwroot", "images", "ObjectImages", res.IdObjectModel + ".png"))));
49+
}
50+
return Json(result);
51+
}
52+
53+
public JsonResult ItemDrilldown(Guid object_model_id)
54+
{
55+
var obj = _ObjectModelService.Get(object_model_id);
56+
if (obj == null) return Json(new { result = "Failure", reason = "No Object was found" });
57+
else
58+
{
59+
ModelObjectJson result = new ModelObjectJson(obj, Url.Content(Path.Combine("wwwroot", "images", "ObjectImages", obj.IdObjectModel + ".png")));
60+
return Json(result);
61+
}
62+
}
63+
64+
[HttpGet]
65+
[Authorize]
66+
public IActionResult GetModelFile(Guid object_model_id)
67+
{
68+
//Grab the model
69+
if (System.IO.File.Exists(Path.Combine("UploadedModles", "User", object_model_id + ".stl")))
70+
{
71+
using (FileStream fs = new FileStream(Path.Combine("UploadedModles", "User", object_model_id + ".stl"), FileMode.Open))
72+
{
73+
return File(fs, "application/octet-stream");
74+
}
75+
}
76+
else return NotFound();
77+
}
78+
79+
//GetAllInfoForItem
80+
//Drilldown for the item. + 3D model for render purposes?
81+
}
82+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
using Microsoft.AspNetCore.Mvc;
22
using System;
33
using System.Collections.Generic;
4+
using System.IO;
45
using System.Linq;
6+
using System.Net;
7+
using System.Net.Sockets;
8+
using System.Runtime.Serialization;
9+
using System.Runtime.Serialization.Formatters.Binary;
10+
using System.Security.Cryptography;
11+
using System.Text;
512
using System.Threading.Tasks;
613

714
namespace _3DPrintingBlockchainMarket.Controllers
815
{
916
public class TransactionController : Controller
1017
{
18+
private string BlockChainPath = Path.Combine("Blockchain", "block-chain.bin");
1119
public TransactionController()
1220
{
1321

1422
}
1523

24+
public bool WriteTransaction(Guid objectModel_id, string User_id)
25+
{
26+
TcpClient client = new TcpClient();
27+
28+
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
29+
30+
client.Connect(serverEndPoint);
31+
32+
NetworkStream clientStream = client.GetStream();
33+
34+
ASCIIEncoding encoder = new ASCIIEncoding();
35+
byte[] buffer = encoder.GetBytes(objectModel_id.ToString() + ";" + User_id);
1636

37+
clientStream.Write(buffer, 0, buffer.Length);
38+
clientStream.Flush();
39+
return true;
40+
}
41+
42+
43+
1744
}
45+
46+
47+
1848
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace _3DPrintingBlockchainMarket.Models.Json
7+
{
8+
public class ModelLicenseJson
9+
{
10+
public ModelLicenseJson(ModelLicense model)
11+
{
12+
id_license = model.IdLicense;
13+
name = model.Name;
14+
description = model.Description;
15+
date_valid_until = model.DateValidUnil;
16+
}
17+
public Guid id_license { get; set; }
18+
public string name { get; set; }
19+
public string description { get; set; }
20+
public DateTime? date_valid_until { get; set; }
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace _3DPrintingBlockchainMarket.Models.Json
7+
{
8+
public class ModelObjectJson
9+
{
10+
public ModelObjectJson(ObjectModel model, string picture_url)
11+
{
12+
id_object_model = model.IdObjectModel;
13+
name = model.Name;
14+
download_count = model.DownloadCount;
15+
object_tags = model.ObjectTags;
16+
token_price = model.TokenPrice;
17+
model_licence = new ModelLicenseJson(model.ModelLicense);
18+
pricing_unit_of_measure = model.PricingUnitOfMeaureId;
19+
20+
}
21+
public Guid id_object_model { get; set; }
22+
public string name { get; set; }
23+
public int download_count { get; set; }
24+
public string object_tags { get; set; }
25+
public decimal? token_price { get; set; }
26+
public ModelLicenseJson model_licence { get; set; }
27+
public string pricing_unit_of_measure { get; set; }
28+
public string picture_url { get; set; }
29+
}
30+
}

3DPrintingBlockchainMarket/Services/ApplicationModelServices.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using _3DPrintingBlockchainMarket.Data;
22
using _3DPrintingBlockchainMarket.Models;
33
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.EntityFrameworkCore;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -12,14 +13,37 @@ namespace _3DPrintingBlockchainMarket.Services
1213
/// Create the Database service for the Object model type using the base implementaiton
1314
/// </summary>
1415
public interface IObjectModelService : IBasicService<ObjectModel>
16+
{
17+
List<ObjectModel> Search(params string[] tags);
18+
}
19+
public interface ILicenseService : IBasicService<ModelLicense>
1520
{
1621

1722
}
23+
24+
public class LicenseService : BasicServiceImplementation<ModelLicense> , ILicenseService
25+
{
26+
public LicenseService(ApplicationDbContext ctx, UserManager<ApplicationUser> um) : base(ctx, um)
27+
{
28+
29+
}
30+
}
1831
public class ObjectModelService : BasicServiceImplementation<ObjectModel> ,IObjectModelService
1932
{
2033
public ObjectModelService(ApplicationDbContext ctx, UserManager<ApplicationUser> um) : base(ctx, um)
2134
{
2235

2336
}
37+
38+
public List<ObjectModel> Search(params string[] tags)
39+
{
40+
List<ObjectModel> res = new List<ObjectModel>();
41+
foreach (var u in tags)
42+
{
43+
res.AddRange(_context.ObjectModel.Include(e => e.PricingUnitOfMeaure).Include(e => e.ModelLicense).Where(e => e.ObjectTags.Contains(u)));
44+
if (res.Count > 20) break;
45+
}
46+
return res;
47+
}
2448
}
2549
}

3DPrintingBlockchainMarket/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public void ConfigureServices(IServiceCollection services)
3838
// Add application services.
3939
services.AddTransient<IEmailSender, EmailSender>();
4040

41+
services.AddScoped<ILicenseService, LicenseService>();
4142
services.AddScoped<IObjectModelService, ObjectModelService>();
4243

4344
services.AddMvc();

3DPrintingBlockchainMarket/UploadedModles/User/0BC929B3-7CDF-43DA-49A5-08D56B5B0210.stl

Whitespace-only changes.
54.1 KB
Loading

0 commit comments

Comments
 (0)