public void CreateEmployee([FromBody] dynamic json) { dynamic temp = JsonConvert.DeserializeObject(json.ToString()); // General employee information string employeeFirstName = temp.Firstname; string employeeLastName = temp.Lastname; string employeeType = temp.Type; double employeeWage = temp.Wage; //Address string employeeLivingAddress = temp.LivingAddress; string employeeZIP = temp.ZIP; string employeeCity = temp.City; string employeeNote = temp.Note; //ContactInfo string employeeEmail = temp.Email; string employeePhoneNumber = temp.Phonenumber; Employee tempEmployee = new Employee(employeeFirstName, employeeLastName, employeeType, employeeWage, new ContactInfo(employeeEmail, employeePhoneNumber), new Address(employeeLivingAddress, employeeZIP, employeeCity, employeeNote)); string available = temp.Available; if (available == "Yes") { tempEmployee.IsAvailable = true; } else { tempEmployee.IsAvailable = false; } database.Employees.Add(tempEmployee); database.SaveChanges(); }
public void EditInspectionReport([FromBody] dynamic json) { dynamic temp = JsonConvert.DeserializeObject(json.ToString()); int inspectionID = temp.ID; InspectionReport inspectionReport = PopulateInspectionReport(temp); inspectionReport.ID = inspectionID; database.Inspections.Update(inspectionReport); database.SaveChanges(); }
public void GetAllCustomers_OnDifferentCustomers_ReturnsAJsonObject() { var options = new DbContextOptionsBuilder <HasserisDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; // Insert seed data into the database using one instance of the context ContactInfo contactInfo = new ContactInfo("*****@*****.**", "41126263"); Address address = new Address("Brandstrupsgade 12", "9000", "Aalborg"); using (var context = new HasserisDbContext(options, true)) { context.Customers.Add(new Private("Christoffer", "Hollensen", address, contactInfo)); context.Customers.Add(new Public(address, contactInfo, "Jammerbugt Kommune", "420133769")); context.Customers.Add(new Business(address, contactInfo, "Skovsgaard Hotel", "32217696969")); context.SaveChanges(); } // Use a clean instance of the context to run the test using (var context = new HasserisDbContext(options, true)) { var service = new CustomerController(context); // getting access to that database var result = service.GetAllCustomers(); //dynamic jsonResult = JsonConvert.DeserializeObject(result.ToString()); Console.WriteLine(result); dynamic json = JsonConvert.DeserializeObject(result); // we check that the type is of JArray Assert.AreEqual(typeof(Newtonsoft.Json.Linq.JArray), json.GetType()); //Assert.IsInstanceOf<Newtonsoft.Json.Linq.JArray>( json.GetType() ); } }
public void GetAllCustomers_OnSomeCustomers_IsNotNull() { // Arrange var options = new DbContextOptionsBuilder <HasserisDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; // Insert seed data into the database using one instance of the context ContactInfo contactInfo = new ContactInfo("*****@*****.**", "41126263"); Address address = new Address("Brandstrupsgade 12", "9000", "Aalborg"); using (var context = new HasserisDbContext(options, true)) { context.Customers.Add(new Private("Christoffer", "Hollensen", address, contactInfo)); context.Customers.Add(new Public(address, contactInfo, "Jammerbugt Kommune", "420133769")); context.Customers.Add(new Business(address, contactInfo, "Skovsgaard Hotel", "32217696969")); context.SaveChanges(); } // Act using (var context = new HasserisDbContext(options, true)) { var service = new CustomerController(context); // getting access to that database var result = service.GetAllCustomers(); // Assert Assert.That(result, Is.Not.Null); } }
public void GetSpecificCustomer_OnExistingPrivateCustomerWithID1_ReturnsThatCustomer() { var options = new DbContextOptionsBuilder <HasserisDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; // Insert data into the database context ContactInfo contactInfo = new ContactInfo("*****@*****.**", "41126263"); Address address = new Address("Brandstrupsgade 12", "9000", "Aalborg"); using (var context = new HasserisDbContext(options, true)) { context.Customers.Add(new Private("Christoffer", "Hollensen", address, contactInfo)); context.Customers.Add(new Public(address, contactInfo, "Jammerbugt Kommune", "420133769")); context.Customers.Add(new Business(address, contactInfo, "Skovsgaard Hotel", "32217696969")); context.SaveChanges(); } // Act // Use a clean instance of the context to run the test using (var context = new HasserisDbContext(options, true)) { var customerController = new CustomerController(context); // getting access to that database // It selects from the same id so it will change the existing object in the database. var actual = customerController.GetSpecificCustomer(1); // Deserialzie our object into a Private type so we can access the properties such as Firstname and assert var actualObj = Newtonsoft.Json.JsonConvert.DeserializeObject <Private>(actual); // Without getting a dependency on the method that retrieves all we will do it manually again. Private customer = (Private)context.Customers.FirstOrDefault(c => c.ID == 1); Assert.That(actualObj.Firstname, Is.EqualTo("Christoffer")); } }
public string uploadImage(dynamic json) { dynamic temp = JsonConvert.DeserializeObject(json.ToString()); string tempBase64 = temp.base64URL; string tempValue = temp.value; int tempIntValue; Int32.TryParse(tempValue, out tempIntValue); string tempLocation = temp.location; string tempType = temp.type; string tempSubType = tempType.Substring(6); string filePath = "..//HasserisWeb/ClientApp/public/assets/images/" + tempLocation + "/" + tempValue + "." + tempSubType; byte[] bytearray = Base64UrlEncoder.DecodeBytes(tempBase64); int arrayCount = bytearray.Length; using (var imageFile = new System.IO.FileStream(filePath, FileMode.Create)) { imageFile.Write(bytearray, 0, arrayCount); imageFile.Flush(); } string newFilePath = "assets/images/" + tempLocation + "/" + tempValue + "." + tempSubType; switch (tempLocation) { case "avatars": var employee = database.Employees.FirstOrDefault(e => e.Username == tempValue); employee.PhotoPath = newFilePath; database.Employees.Update(employee); database.SaveChanges(); break; case "tasks": var task = database.Tasks.FirstOrDefault(t => t.ID == tempIntValue); task.PhotoPath = newFilePath; database.Tasks.Update(task); database.SaveChanges(); break; //cases goes here default: throw new DriveNotFoundException("Destination folder does not exist."); } return(newFilePath); }
public void EditDeliveryTask([FromBody] dynamic json) { dynamic temp = JsonConvert.DeserializeObject(json.ToString()); int deliveryID = temp.ID; Delivery delivery = PopulateDeliveryTask(temp); Delivery tempDelivery = (Delivery)database.Tasks.FirstOrDefault(i => i.ID == deliveryID); Offer tempOffer = new Offer(); if (tempDelivery.Offer != null) { tempOffer = database.Offers.FirstOrDefault(o => o.ID == tempDelivery.Offer.ID); delivery.Offer = tempOffer; } InspectionReport inspection = new InspectionReport(); if (tempDelivery.InspectionReport != null) { inspection = database.Inspections.FirstOrDefault(i => i.ID == tempDelivery.InspectionReport.ID); delivery.InspectionReport = inspection; } tempDelivery = delivery; tempDelivery.ID = deliveryID; database.Update(tempDelivery); database.SaveChanges(); }
public string RemoveTask([FromBody] dynamic json) { dynamic deserializeObject = JsonConvert.DeserializeObject(json.ToString()); int id = deserializeObject.eventId; var task = database.Tasks.Include(t => t.Dates). Include(t => t.PauseTimes). Include(t => t.Employees). Include(t => t.Equipment).FirstOrDefault(t => t.ID == id); database.Tasks.RemoveRange(task); database.Tasks.Remove(task); database.SaveChanges(); return("OK - removed id#" + id); }
public void CreateTool([FromBody] dynamic json) { dynamic temp = JsonConvert.DeserializeObject(json.ToString()); Tool tool = new Tool(); tool.Name = temp.Name; string available = temp.Available; if (available == "Yes") { tool.IsAvailable = true; } else { tool.IsAvailable = false; } database.Equipment.Add(tool); database.SaveChanges(); }
public void EditOffer([FromBody] dynamic json) { dynamic temp = JsonConvert.DeserializeObject(json.ToString()); int offerID = temp.ID; Offer offer = PopulateOffer(temp); offer.Sent = temp.Sent; offer.InvoiceSent = temp.InvoiceSent; offer.ID = offerID; int lentBoxes = temp.Lentboxes; int expectedHours = temp.ExpectedHours; offer.Lentboxes = lentBoxes; offer.ExpectedHours = expectedHours; offer.InspectionReportID = temp.InspectionReportID; offer.WithPacking = temp.WithPacking; offer.WasInspection = temp.WasInspection; offer.Lentboxes = lentBoxes; database.Offers.Update(offer); database.SaveChanges(); }
public void CreateVehicle([FromBody] dynamic json) { dynamic temp = JsonConvert.DeserializeObject(json.ToString()); string vehicleName = temp.Name; string vehicleModel = temp.Model; string vehicleRegNum = temp.RegNum; Vehicle vehicle = new Vehicle(vehicleName, vehicleModel, vehicleRegNum); string available = temp.Available; if (available == "Yes") { vehicle.IsAvailable = true; } else { vehicle.IsAvailable = false; } database.Equipment.Add(vehicle); database.SaveChanges(); }
public string CreateFurniture([FromBody] dynamic json) { dynamic eNewFurniture = JsonConvert.DeserializeObject(json.ToString()); string furnitureName = eNewFurniture.newFurniture.name; double furnitureCubicSize = eNewFurniture.newFurniture.cubicSize; string furnitureType = eNewFurniture.newFurniture.type; double furnitureWeight = eNewFurniture.newFurniture.weight; Furniture furniture = new Furniture(furnitureName, furnitureCubicSize, furnitureType, furnitureWeight); database.Furniture.Add(furniture); database.SaveChanges(); return("Succesfully added new tool"); }
public void EditBusinessCustomer_OnBusinessCustomer_ReturnsAnEditedObject() { var options = new DbContextOptionsBuilder <HasserisDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; // Insert data into the database context ContactInfo contactInfo = new ContactInfo("*****@*****.**", "41126263"); Address address = new Address("Brandstrupsgade 12", "9000", "Aalborg"); using (var context = new HasserisDbContext(options, true)) { context.Customers.Add(new Private("Christoffer", "Hollensen", address, contactInfo)); context.Customers.Add(new Public(address, contactInfo, "Jammerbugt Kommune", "420133769")); context.Customers.Add(new Business(address, contactInfo, "Skovsgaard Hotel", "32217696969")); context.SaveChanges(); } // Object to edit Business customerToEdit = new Business(address, contactInfo, "Fønix Hotel", "420133769"); // We change the ID the customerToEdit object, so we get the same customer to edit customerToEdit.ID = 3; // Act // Use a clean instance of the context to run the test using (var context = new HasserisDbContext(options, true)) { var customerController = new CustomerController(context); // getting access to that database // We have to serialize our object as JSON so it looks like the one being sent from our view component. string json = JsonConvert.SerializeObject(customerToEdit, Formatting.Indented); // It selects from the same id so it will change the existing object in the database. customerController.EditBusinessCustomer(json); // Without getting a dependency on the method that retrieves all we will do it manually again. Business customer = (Business)context.Customers.FirstOrDefault(c => c.ID == customerToEdit.ID); //dynamic jsonResult = JsonConvert.DeserializeObject("asd".ToString()); // Get all customers to get reference to old one // Assert that the old one and the edited customer are the same by looking at the name cuz we edited that information Assert.That(customerToEdit.Name, Is.EqualTo("Fønix Hotel")); } }
public void AddPrivateCustomer([FromBody] dynamic json) { dynamic temp = JsonConvert.DeserializeObject(json.ToString()); string address = temp.LivingAddress; string zip = temp.ZIP; string city = temp.City; Address livingaddress = new Address(address, zip, city); string email = temp.Email; string phonenumber = temp.Phonenumber; ContactInfo contactinfo = new ContactInfo(email, phonenumber); string firstname = temp.Firstname; string lastname = temp.Lastname; Private privateCustomer = new Private(firstname, lastname, livingaddress, contactinfo); database.Customers.Add(privateCustomer); database.SaveChanges(); }
public void DeleteCustomer_OnLegitCustomer_ReturnsNewDBMinusTheCustomer() { var options = new DbContextOptionsBuilder <HasserisDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; // Insert data into the database context ContactInfo contactInfo = new ContactInfo("*****@*****.**", "41126263"); Address address = new Address("Brandstrupsgade 12", "9000", "Aalborg"); using (var context = new HasserisDbContext(options, true)) { context.Customers.Add(new Private("Christoffer", "Hollensen", address, contactInfo)); context.Customers.Add(new Public(address, contactInfo, "Jammerbugt Kommune", "420133769")); context.Customers.Add(new Business(address, contactInfo, "Skovsgaard Hotel", "32217696969")); context.SaveChanges(); } // Object to delete Private customerToDelete = new Private("Christoffer", "Hollensen", address, contactInfo); // We change the ID the customerToEdit object, so we get the same customer to delete customerToDelete.ID = 1; // Act // Use a clean instance of the context to run the test using (var context = new HasserisDbContext(options, true)) { var customerController = new CustomerController(context); // getting access to that database // We have to serialize our object as JSON so it looks like the one being sent from our view component. string json = JsonConvert.SerializeObject(customerToDelete, Formatting.Indented); // It selects from the same id so it will change the existing object in the database. customerController.DeleteCustomer(json); // Without getting a dependency on the other method that retrieves all, we will do it manually again. Private customer = (Private)context.Customers.FirstOrDefault(c => c.ID == customerToDelete.ID); Assert.That(context.Customers.Contains(customer), Is.EqualTo(false)); } }
public void AddPrivateCustomer_AddsLegitPrivateCustomer_ReturnsNewDBPlusCustomer() { var options = new DbContextOptionsBuilder <HasserisDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; // Insert data into the database context ContactInfo contactInfo = new ContactInfo("*****@*****.**", "41126263"); Address address = new Address("Brandstrupsgade 12", "9000", "Aalborg"); using (var context = new HasserisDbContext(options, true)) { context.Customers.Add(new Private("Christoffer", "Hollensen", address, contactInfo)); context.SaveChanges(); } // Declare object to add Private customerToAdd = new Private("CholleAdded", "Holle", address, contactInfo); customerToAdd.ID = 2; // Serialize object so it can be used in our controller method var customerToAddJson = Newtonsoft.Json.JsonConvert.SerializeObject(customerToAdd); // Act // Use a clean instance of the context to run the test using (var context = new HasserisDbContext(options, true)) { var customerController = new CustomerController(context); // getting access to that database // It selects from the same id so it will change the existing object in the database. customerController.AddPrivateCustomer(customerToAddJson); // Without getting a dependency on the method that retrieves all we will do it manually again. Private customer = (Private)context.Customers.FirstOrDefault(c => c.ID == 2); Assert.That(customer.Firstname, Is.EqualTo("CholleAdded")); } }
public string Get(dynamic json) { dynamic tempstring = JsonConvert.DeserializeObject(json.ToString()); string username = tempstring.name; string password = tempstring.pass; if (CheckUser(username, password)) { returnObjects.access_token = GenerateToken(username); var employee = database.Employees .Include(contact => contact.ContactInfo) .Include(address => address.Address) .FirstOrDefault(e => e.Username == username);; employee.AccessToken = returnObjects.access_token; database.Employees.Update(employee); database.SaveChanges(); } return(JsonConvert.SerializeObject(returnObjects)); }
public SystemControl() { using (var db = new HasserisDbContext()) { db.Database.EnsureDeleted(); db.Database.EnsureCreated(); Employee tempEmployee = new Employee("Christopher", "Chollesen", "AdminPlus", 120, new ContactInfo("*****@*****.**", "41126263"), new Address("Sohngaardsholmparken", "9000", "Aalborg", "Til højre")); tempEmployee.AddLoginInfo("Cholle", "Cholle17"); tempEmployee.IsAvailable = true; Employee tempEmployee_one = new Employee("Jakob", "Østenkjær", "AdminPlus", 150, new ContactInfo("*****@*****.**", "28943519"), new Address("Herningvej", "9220", "Aalborg", "Til højre")); tempEmployee_one.AddLoginInfo("Snuuby", "Jakob17"); tempEmployee_one.IsAvailable = true; Employee tempEmployee_jakob = new Employee("Jacob", "Hasseris", "AdminPlus", 210, new ContactInfo("*****@*****.**", "30103010"), new Address("HasserisVej", "9220", "Aalborg", "Til højre")); tempEmployee_one.AddLoginInfo("Jacob", "Jacob17"); tempEmployee_one.IsAvailable = true; db.Employees.Add(tempEmployee_jakob); Employee tempEmployee_two = new Employee("Kristian", "Eriksen", "AdminPlus", 150, new ContactInfo("*****@*****.**", "50734649"), new Address("Herningvej", "9220", "Aalborg", "Til højre")); tempEmployee_two.AddLoginInfo("Kristian", "Kristian17"); tempEmployee_two.IsAvailable = true; Employee tempEmployee_three = new Employee("Daniel", "Heilskov", "AdminPlus", 150, new ContactInfo("*****@*****.**", "42438049"), new Address("Herningvej", "9220", "Aalborg", "Til højre")); tempEmployee_three.AddLoginInfo("Daniel", "Daniel17"); tempEmployee_three.IsAvailable = true; Employee tempEmployee_four = new Employee("Simon", "Kanne", "AdminPlus", 150, new ContactInfo("*****@*****.**", "61466211"), new Address("Herningvej", "9220", "Aalborg", "Til højre")); tempEmployee_four.AddLoginInfo("Simon", "Simon17"); tempEmployee_four.IsAvailable = true; Employee tempEmployee_five = new Employee("Mathias", "Møller", "AdminPlus", 150, new ContactInfo("*****@*****.**", "93958200"), new Address("Herningvej", "9220", "Aalborg", "Til højre")); tempEmployee_five.AddLoginInfo("Mathias", "Møller17"); tempEmployee_five.IsAvailable = true; Employee tempEmployee_six = new Employee("Andreas", "Nichum", "Employee", 150, new ContactInfo("*****@*****.**", "24840884"), new Address("Herningvej", "9220", "Aalborg", "Til højre")); tempEmployee_six.AddLoginInfo("Andreas", "Andreas17"); tempEmployee_six.IsAvailable = true; Customer tempCustomer = new Private("Erik", "Larsen", new Address("Aalborg Vej", "9220", "Aalborg", "Første dør til højre"), new ContactInfo("*****@*****.**", "23131313")); Customer tempCustomer_one = new Public(new Address("Aalborghusvej", "9110", "Aalborg", "Anden dør"), new ContactInfo("*****@*****.**", "23131313"), "Hasseris Flytteforetning", "123123123123"); Furniture tempFurniture = new Furniture("Sofa møbel", 10, "Sofa", 10); Equipment testEquipment = new Vehicle("Stor lastbil", "Opel", "13131313"); Equipment testEquipment_one = new Vehicle("Alimndelig bil", "Citroen", "13131313"); testEquipment.IsAvailable = true; testEquipment_one.IsAvailable = true; List <DateTime> testList = new List <DateTime>() { new DateTime(2019, 11, 13), new DateTime(2019, 11, 14) }; List <DateTime> testList_two = new List <DateTime>() { new DateTime(2019, 11, 03), new DateTime(2019, 11, 04) }; Delivery tempDelivery = new Delivery("Test Delivery", tempCustomer, new Address("Hasseris vej", "9220", "Aalborg", "Tredje dør til venstre"), 600, testList, "Giv erik noget", "28313131", "Foam", 5, 3); foreach (DateTime date in testList) { PauseTimes temp = new PauseTimes(); temp.Date = date; tempDelivery.PauseTimes.Add(temp); } Moving tempMoving = new Moving("Test Moving", tempCustomer_one, new Address("Kukux vej", "9000", "Aalborg", "første dør til venstre"), 700, testList_two, "Hjælp Lars med at flytte", "23131343", tempCustomer_one.Address, 5, true, 3); tempMoving.Furnitures.Add(tempFurniture); foreach (DateTime date in testList_two) { PauseTimes temp = new PauseTimes(); temp.Date = date; tempMoving.PauseTimes.Add(temp); } tempMoving.Equipment.Add(new TaskAssignedEquipment() { Equipment = testEquipment }); tempMoving.Equipment.Add(new TaskAssignedEquipment() { Equipment = testEquipment_one }); tempMoving.Employees.Add(new TaskAssignedEmployees() { Employee = tempEmployee }); tempMoving.Employees.Add(new TaskAssignedEmployees() { Employee = tempEmployee_five }); tempMoving.Employees.Add(new TaskAssignedEmployees() { Employee = tempEmployee_one }); tempDelivery.Employees.Add(new TaskAssignedEmployees() { Employee = tempEmployee_six }); tempDelivery.Employees.Add(new TaskAssignedEmployees() { Employee = tempEmployee_two }); tempDelivery.Equipment.Add(new TaskAssignedEquipment() { Equipment = testEquipment_one }); db.Tasks.Add(tempDelivery); db.Tasks.Add(tempMoving); db.SaveChanges(); /* * db.Employees.Add(tempEmployee); * db.Employees.Add(tempEmployee_one); * db.Employees.Add(tempEmployee_two); * db.Employees.Add(tempEmployee_three); * db.Employees.Add(tempEmployee_four); * db.Employees.Add(tempEmployee_five); * db.Employees.Add(tempEmployee_six); * * db.Customers.Add(tempCustomer); * db.Customers.Add(tempCustomer_one); * * db.Equipment.Add(testEquipment); * db.Equipment.Add(testEquipment_one); * db.Furniture.Add(tempFurniture); */ foreach (Task ctxTask in db.Tasks) { Console.WriteLine($"{ctxTask.Name}:"); if (ctxTask.Customer is Private) { Console.WriteLine($"{ctxTask.Customer.ID}: {((Private)ctxTask.Customer).Firstname} {((Private)ctxTask.Customer).Lastname}"); } else if (ctxTask.Customer is Business) { Console.WriteLine($"{ctxTask.Customer.ID}: {((Business)ctxTask.Customer).Name}"); } else { Console.WriteLine($"{((Public)ctxTask.Customer).ID}: {((Public)ctxTask.Customer).Name}"); } foreach (var employee in ctxTask.Employees.Select(e => e.Employee)) { Console.WriteLine($"{employee.ID}: {employee.Firstname} {employee.Lastname}"); } foreach (var equipment in ctxTask.Equipment.Select(e => e.Equipment)) { Console.WriteLine($"{equipment.ID}: {equipment.Name}"); } if (ctxTask is Moving) { Console.WriteLine($"Furniture:"); foreach (var furniture in ((Moving)ctxTask).Furnitures) { Console.WriteLine($"{furniture.ID}: {furniture.Name}"); } } Console.WriteLine($"Date(s): "); foreach (var date in ctxTask.Dates) { Console.WriteLine($" {date.Date.DayOfYear}"); } } var test = db.Employees.FirstOrDefault(); Console.WriteLine(test.ContactInfo.Email); } }