-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrapController.cs
135 lines (116 loc) · 4.31 KB
/
WrapController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using Preveld.Infrastructure;
using Preveld.Models;
using Preveld.ViewModels;
using System.Collections.Generic;
using System.Data.Entity;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using QRCoder;
namespace Preveld.Controllers
{
public class WrapController : BaseController
{
private ApplicationDBContext db = new ApplicationDBContext();
[CustomAuthorize]
public ActionResult Index()
{
List<Wrap> lists = db.Wraps.OrderByDescending(w => w.Date_of_last_Inspection).ToList();
return View(lists);
}
[CustomAuthorize]
public ActionResult Create()
{
WrapHistoryViewModel wrapHistoryViewModel = new WrapHistoryViewModel();
wrapHistoryViewModel.Wrap = new Wrap();
wrapHistoryViewModel.Wraps = db.Wraps.OrderByDescending(x => x.Date_of_last_Inspection).ToList();
return View(wrapHistoryViewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
[CustomAuthorize]
public ActionResult Create(WrapHistoryViewModel wrapHistoryViewModel)
{
var wrap = wrapHistoryViewModel.Wrap;
if (ModelState.IsValid)
{
db.Wraps.Add(wrap);
db.SaveChanges();
return RedirectToAction("Index");
}
wrapHistoryViewModel.Wraps = db.Wraps.OrderByDescending(x => x.Date_of_last_Inspection).ToList();
return View(wrapHistoryViewModel);
}
[CustomAuthorize]
public ActionResult Show(int ID)
{
WrapHistoryViewModel wrapHistoryViewModel = new WrapHistoryViewModel();
var currentWrap = db.Wraps.Find(ID);
wrapHistoryViewModel.Wrap = currentWrap;
wrapHistoryViewModel.Wraps = db.Wraps.OrderByDescending(x => x.Date_of_last_Inspection).ToList();
var nextWrap = db.Wraps.Where(x => x.Date_of_last_Inspection < currentWrap.Date_of_last_Inspection).OrderByDescending(x => x.Date_of_last_Inspection).FirstOrDefault();
if (nextWrap != null)
{
ViewBag.NextRecordID = nextWrap.ID;
} else
{
ViewBag.NextRecordID = null;
}
var prevWrap = db.Wraps.Where(x => x.Date_of_last_Inspection > currentWrap.Date_of_last_Inspection).OrderBy(x => x.Date_of_last_Inspection).FirstOrDefault();
if (prevWrap != null)
{
ViewBag.PrevRecordID = prevWrap.ID;
}
else
{
ViewBag.PrevRecordID = null;
}
var plainText = "{\"wrap\":" + currentWrap.ID + "}";
QRCodeGenerator _qrCode = new QRCodeGenerator();
QRCodeData _qrCodeData = _qrCode.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(_qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
wrapHistoryViewModel.QrCode = BitmapToBytesCode(qrCodeImage);
return View(wrapHistoryViewModel);
}
[CustomAuthorize]
public ActionResult Edit(int ID)
{
var wrap = db.Wraps.Find(ID);
return View(wrap);
}
[CustomAuthorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Wrap wrap)
{
if (ModelState.IsValid)
{
db.Entry(wrap).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(wrap);
}
[CustomAuthorize]
public ActionResult Delete(int ID)
{
var wrap = db.Wraps.Find(ID);
db.Wraps.Remove(wrap);
db.SaveChanges();
return RedirectToAction("Index");
}
[NonAction]
private static Byte[] BitmapToBytesCode(Bitmap image)
{
using (MemoryStream stream = new MemoryStream())
{
image.Save(stream, ImageFormat.Png);
return stream.ToArray();
}
}
}
}