I knew a little Java, and thought what the hell, I'll help my school and design this program that'll help checkout phone chargers and calculators and things to students. So I got in way over my head, but I got the program working. I wrote it in C#, and while the whole thing needs review, I really feel my checkout function could use immediate improvement.
This is the controller function for creating a checkout record, I have 6 different tables:
tb_BannedUsersTable
tb_CheckoutCheckin
tb_Items
tb_LabTech
(employees)tb_Student
It starts by taking a Student ID and up-to 3 items from an HTML view to create a record. it creates a new record per item, because of how everything gets checked in. It's a long garbled mess, so any advice is greatly appreciated.
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(string StudentID, string upc, string upc1, string upc2)
{
try
{
ViewModels view = new ViewModels();
Student_Entities ExternalDB = new Students_Entities();
tb_Student sfh = new tb_Student();
if (StudentID.Length > 9)
{
//Shortens the scanned StudentID to 9 characters
StudentID = StudentID.Truncate(9);
}
// Checks for Student record in the exsisting database
var queryCount = db.tb_Student.Where(s => s.ID == StudentID).Count();
if (queryCount < 1)
{
// checks external database of students
try
{
var bsh = ExternalDB.v_View.FirstOrDefault(s => s.ID == StudentID);
if (bsh == null)
{
ViewBag.Message = "Student ID # not found in the system, please add the student from the Home menu";
return View("Index");
}
//creates student record if its found
sfh.ID = bsh.ID;
sfh.EMAIL_ADDRESS = bsh.EMAIL_ADDRESS;
sfh.UserNAME = bsh.UserNAME;
sfh.FIRST_NAME = bsh.FIRST_NAME;
sfh.LAST_NAME = bsh.LAST_NAME;
sfh.PHONE = bsh.LAST_NAME;
sfh.SIGNEDWAIVER = null;
db.tb_Student.Add(sfh);
db.SaveChanges();
}
catch
{
ViewBag.Message = "Student ID # not found in the system, please add the student from the Home menu";
return View("Index");
}
}
var tbs = db.tb_Student.FirstOrDefault(s => s.ID == StudentID);
bool? x = tbs.SIGNEDWAIVER;
//Checks for signed Waiver
//Stores SessionVariables.
if (x != true)
{
SessionVariables.waiverStudentId = StudentID;
SessionVariables.waiverUPC = upc;
SessionVariables.waiverUPC1 = upc1;
SessionVariables.waiverUPC2 = upc2;
ViewBag.Message = "Records indicate this user has not signed a waiver";
return View("Waiver", tbs);
}
if (upc1 == "") { upc1 = null; }
if (upc2 == "") { upc2 = null; }
//checks for UPC Entry
if (upc == null)
{
ViewBag.Message = "Item Number cannot be blank with checkout";
return View("Index");
}
if (upc == upc1 || upc == upc2)
{
ViewBag.Message = "Item Number was the same in both fields";
return View("Index");
}
//checks for banned users
if (!view.isBannedUser(StudentID))
{
//user is good, check for vaild Items
//Item 1 Check
var itemCheckout = db.tb_Items.Where(s => s.ItemUPC == upc).FirstOrDefault();
var checkedOut = db.tb_CheckoutCheckin.Where(s => s.ItemUPCFK == upc && s.CheckinDate == null).FirstOrDefault();
if (itemCheckout == null)
{
ViewBag.Message = "Item #1 " + upc + " number not found";
return View("Index");
}
if (checkedOut != null)
{
ViewBag.Message = "Our Records indicate that item" + upc + " is checked-out, please check the item in first! " + StudentID;
return View("Index");
}
//Item 2 Check
if (upc1 != null)
{
var itemCheckout1 = db.tb_Items.Where(s => s.ItemUPC == upc1).FirstOrDefault();
var checkedOut1 = db.tb_CheckoutCheckin.Where(s => s.ItemUPCFK == upc1 && s.CheckinDate == null).FirstOrDefault();
if (itemCheckout1 == null)
{
ViewBag.Message = "Item #2 " + upc1 + " not found";
return View("Index");
}
if (checkedOut1 != null)
{
ViewBag.Message = "Our Records indicate that item #2 " + upc1 + " is checked-out, please check the item in first! " + StudentID;
return View("Index");
}
}
//Item 3 Check
if (upc2 != null)
{
var itemCheckout2 = db.tb_LabInventoryItems.Where(s => s.ItemUPC == upc2).FirstOrDefault();
var checkedOut2 = db.tb_CheckoutCheckin.Where(s => s.ItemUPCFK == upc2 && s.CheckinDate == null).FirstOrDefault();
if (itemCheckout2 == null)
{
ViewBag.Message = "Item #3 " + upc2 + " not found";
return View("Index");
}
if (checkedOut2 != null)
{
ViewBag.Message = "Our Records indicate that item #3 " + upc2 + " is checked-out, please check the item in first! " + StudentID;
return View("Index");
}
}
var checkoutRecord = new tb_CheckoutCheckin();
var checkoutRecord1 = new tb_CheckoutCheckin();
var checkoutRecord2 = new tb_CheckoutCheckin();
var stuRec = db.tb_Student.FirstOrDefault(s => s.ID == StudentID);
var stuFirst = stuRec.FIRST_NAME + " " + stuRec.LAST_NAME;
//One item Checkout
if (upc != null && StudentID != null && upc1 == null && upc2 == null)
{
checkoutRecord.Student_IDFK = StudentID;
checkoutRecord.ItemUPCFK = upc;
checkoutRecord.ItemIDFK = itemCheckout.ItemId;
checkoutRecord.CheckoutLabTech = SessionVariables.CurrentUserId;
checkoutRecord.CheckoutDate = DateTime.Now;
db.tb_CheckoutCheckin.Add(checkoutRecord);
db.SaveChanges();
ModelState.Clear();
var model = db.tb_CheckoutCheckin.Where(s => s.CheckinDate == null);
ViewBag.Message = stuFirst + " " + upc + " due back at " + SessionVariables.ItemDueDateTime;
return View("Index", model);
}
//two item Checkout
if (upc != null && StudentID != null && upc1 != null && upc2 == null)
{
checkoutRecord.Student_IDFK = StudentID;
checkoutRecord.ItemUPCFK = upc;
checkoutRecord.ItemIDFK = itemCheckout.ItemId;
checkoutRecord.CheckoutLabTech = SessionVariables.CurrentUserId;
checkoutRecord.CheckoutDate = DateTime.Now;
checkoutRecord1.Student_IDFK = StudentID;
checkoutRecord1.ItemUPCFK = upc1;
checkoutRecord1.ItemIDFK = itemCheckout.ItemId;
checkoutRecord1.CheckoutLabTech = SessionVariables.CurrentUserId;
checkoutRecord1.CheckoutDate = DateTime.Now;
db.tb_CheckoutCheckin.Add(checkoutRecord);
db.tb_CheckoutCheckin.Add(checkoutRecord1);
db.SaveChanges();
ModelState.Clear();
var model = db.tb_CheckoutCheckin.Where(s => s.CheckinDate == null);
ViewBag.Message = stuFirst + " " + upc + " and " + upc1 + " due back at " + SessionVariables.ItemDueDateTime;
return View("Index", model);
}
//three item Checkout
if (upc != null && StudentID != null && upc1 != null && upc2 != null)
{
checkoutRecord.Student_IDFK = StudentID;
checkoutRecord.ItemUPCFK = upc;
checkoutRecord.ItemIDFK = itemCheckout.ItemId;
checkoutRecord.CheckoutLabTech = SessionVariables.CurrentUserId;
checkoutRecord.CheckoutDate = DateTime.Now;
checkoutRecord1.Student_IDFK = StudentID;
checkoutRecord1.ItemUPCFK = upc1;
checkoutRecord1.ItemIDFK = itemCheckout.ItemId;
checkoutRecord1.CheckoutLabTech = SessionVariables.CurrentUserId;
checkoutRecord1.CheckoutDate = DateTime.Now;
checkoutRecord2.Student_IDFK = StudentID;
checkoutRecord2.ItemUPCFK = upc2;
checkoutRecord2.ItemIDFK = itemCheckout.ItemId;
checkoutRecord2.CheckoutLabTech = SessionVariables.CurrentUserId;
checkoutRecord2.CheckoutDate = DateTime.Now;
db.tb_CheckoutCheckin.Add(checkoutRecord);
db.tb_CheckoutCheckin.Add(checkoutRecord1);
db.tb_CheckoutCheckin.Add(checkoutRecord2);
db.SaveChanges();
ModelState.Clear();
var model = db.tb_CSUCheckoutCheckin.Where(s => s.CheckinDate == null);
ViewBag.Message = stuFirst + " " + upc + " and " + upc1 + " and " + upc2 + " due back at " + SessionVariables.ItemDueDateTime;
return View("Index", model);
}
}
ViewBag.Message = "That user has been banned from checkout! Or you got this page in error, try again. " + StudentID;
return View("Index");
}
catch
{
ViewBag.Message = "An error occured during Checkout, please try again, Student Id." + StudentID;
return View("Index");
}
}
// GET: tb_Checkout/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tb_CSUCheckoutCheckin tb_CSUCheckoutCheckin = db.tb_CSUCheckoutCheckin.Find(id);
if (tb_CSUCheckoutCheckin == null)
{
return HttpNotFound();
}
return View(tb_CSUCheckoutCheckin);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CheckoutCheckinId,CSU_IDFK,ItemUPCFK,CheckoutLabTech,CheckoutDate,CheckinLabTech,CheckinDate,CheckoutLocationFK,CheckinLocationFK")] tb_CSUCheckoutCheckin tb_CSUCheckoutCheckin)
{
if (ModelState.IsValid)
{
db.Entry(tb_CSUCheckoutCheckin).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tb_CSUCheckoutCheckin);
}
//alter record for waiver signed
public ActionResult WaiverSign(string id)
{
var v = db.tb_Student.FirstOrDefault(s => s.CSU_ID == id);
v.SIGNEDWAIVER = true;
db.Entry(v).State = EntityState.Modified;
ViewBag.Message = "Waiver Recorded, and Checkout Complete!";
return Create(SessionVariables.waiverStudnetID, SessionVariables.waiverUPC, SessionVariables.waiverUPC1, SessionVariables.waiverUPC2);
}
I figure there has got to be a more efficient way to handle all this, and I'm not really sure how to create a 'data access layer' and I'm probably breaking some security rules, so the more help the better. My Checkin
function isn't any better.