/// <summary> /// Inserts a Company into the database /// </summary> /// <param name="company"></param> /// <returns>Returns true if successful, false if an entry with the same id (pk) already exists</returns> public bool InsertCompany(Company company) { //db.Table<Company>() System.Diagnostics.Debug.WriteLine("DbCompany - InsertCompany: Initiated"); try { // if exist company will not be inserted. lock (DbContext.locker) { var checkIfExists = Db.Get<Company>(company.id); } System.Diagnostics.Debug.WriteLine("DbCompany - InsertCompany: Company already exists"); return false; } catch (Exception e) { // if not exist company will be inserted. lock (DbContext.locker) { System.Diagnostics.Debug.WriteLine("DbCompany - InsertCompany: before insert"); Db.Insert(company); System.Diagnostics.Debug.WriteLine("DbCompany - InsertCompany: successfully inserted"); return true; } } }
/// <summary> /// Deserializes Company from web api data. /// /// IMPORTANT: Only values used for notification list will work at this implementation. /// </summary> /// <param name="companyDict"></param> /// <returns></returns> public Company DeserializeCompany(Dictionary<string, object> companyDict) { Company company = new Company(); System.Diagnostics.Debug.WriteLine("companyDict created"); company.id = companyDict["id"].ToString(); company.id = Hasher.Base64Encode(company.id); if (companyDict.ContainsKey("name")) { company.name = companyDict["name"].ToString(); } if (companyDict.ContainsKey("modified")) { company.name = companyDict["modified"].ToString(); } if (companyDict.ContainsKey("logo")) { company.logo = companyDict["logo"].ToString(); } return company; }
/// <summary> /// Updates a Company, but if it doesnt already exist a new entry will be inserted into the db. /// </summary> /// <param name="company"></param> public void UpdateCompany(Company company) { try { // if exist company will be updated. lock (DbContext.locker) { var checkIfExists = Db.Get<Company>(company.id); Db.Update(company); } } catch (Exception e) { // if not exist company will be inserted. lock (DbContext.locker) { Db.Insert(company); } } }
private async void TestInsertProject(object sender, EventArgs e) { DbContext DbContext = DbContext.GetDbContext; SQLiteConnection Db = DbContext.Db; System.Diagnostics.Debug.WriteLine("Before insert Project.Count: " + Db.Query<Project>("Select * from Project").Count()); ProjectsController jc = new ProjectsController(); string testUuid = "colemak"; Project project = new Project() { uuid = testUuid, }; string companyId = "Ikea"; Company comp = new Company() { id = companyId }; string courseId = "sverige"; Course course = new Course() { id = courseId }; string sgId = "dykking"; StudyGroup sg = new StudyGroup() { id = sgId }; StudyGroupProject sgj = new StudyGroupProject() { StudyGroupId = sgId, ProjectUuid = testUuid }; CourseProject lj = new CourseProject() { CourseId = courseId, ProjectUuid = testUuid }; CompanyProject cp = new CompanyProject() { CompanyId = companyId, ProjectUuid = testUuid }; string jtId = "10aarErfaringEcma6"; JobType jt = new JobType() { id = jtId }; JobTypeProject jtp = new JobTypeProject() { ProjectUuid = testUuid, JobTypeId = jtId }; // try catch on tables that will not be affected by a job delete try { Db.Insert(comp); } catch { } Db.Insert(project); try { Db.Insert(course); } catch { } try { Db.Insert(sg); } catch { } Db.Insert(sgj); Db.Insert(lj); Db.Insert(cp); try { Db.Insert(jt); } catch { } Db.Insert(jtp); System.Diagnostics.Debug.WriteLine("After insert: Project.Count: " + Db.Query<Project>("Select * from Project").Count()); }
private async void TestInsertJob(object sender, EventArgs e) { DbContext DbContext = DbContext.GetDbContext; SQLiteConnection Db = DbContext.Db; System.Diagnostics.Debug.WriteLine("Before insert Job.Count: " + Db.Query<Job>("Select * from Job").Count()); JobsController jc = new JobsController(); DateTime yesterday = DateTime.Now.AddDays(-1); long n = long.Parse(yesterday.ToString("yyyyMMddHHmmss")); string testUuid = "colemak"; Job job = new Job() { uuid = testUuid, expiryDate = n }; string companyId = "Ikea"; Company comp = new Company() { id = companyId }; string locationId = "sverige"; Location loc = new Location() { id = locationId }; string sgId = "dykking"; StudyGroup sg = new StudyGroup() { id = sgId }; StudyGroupJob sgj = new StudyGroupJob() { StudyGroupId = sgId, JobUuid = testUuid }; LocationJob lj = new LocationJob() { LocationId = locationId, JobUuid = testUuid }; CompanyJob cj = new CompanyJob() { CompanyId = companyId, JobUuid = testUuid }; string jtId = "10aarErfaringEcma6"; JobType jt = new JobType() { id = jtId }; JobTypeJob jtj = new JobTypeJob() { JobUuid = testUuid, JobTypeId = jtId }; // try catch on tables that will not be affected by a job delete try { Db.Insert(comp); } catch { } Db.Insert(job); try { Db.Insert(loc); } catch { } try { Db.Insert(sg); } catch { } Db.Insert(sgj); Db.Insert(lj); Db.Insert(cj); try { Db.Insert(jt); } catch { } Db.Insert(jtj); System.Diagnostics.Debug.WriteLine("After insert: Job.Count: " + Db.Query<Job>("Select * from Job").Count()); }
/// <summary> /// This test method require that you have not logged in and got no authorization /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void TestDeleteExpiredJobs(object sender, EventArgs e) { DbJob jc = new DbJob(); DbContext DbContext = DbContext.GetDbContext; SQLiteConnection Db = DbContext.Db; DateTime yesterday = DateTime.Now.AddDays(-1); long n = long.Parse(yesterday.ToString("yyyyMMddHHmmss")); string testUuid = "colemak"; Job job = new Job() { uuid = testUuid, expiryDate = n }; string companyId = "Ikea"; Company comp = new Company() { id = companyId }; string locationId = "sverige"; Location loc = new Location() { id = locationId }; string sgId = "dykking"; StudyGroup sg = new StudyGroup() { id = sgId }; StudyGroupJob sgj = new StudyGroupJob() { StudyGroupId = sgId, JobUuid = testUuid }; LocationJob lj = new LocationJob() { LocationId = locationId, JobUuid = testUuid }; CompanyJob cj = new CompanyJob() { CompanyId = companyId, JobUuid = testUuid }; string jtId = "10aarErfaringEcma6"; JobType jt = new JobType() { id = jtId }; JobTypeJob jtj = new JobTypeJob() { JobUuid = testUuid, JobTypeId = jtId }; Db.Insert(comp); Db.Insert(job); Db.Insert(loc); Db.Insert(sg); Db.Insert(sgj); Db.Insert(lj); Db.Insert(cj); Db.Insert(jt); Db.Insert(jtj); Job j = Db.Get<Job>(testUuid); System.Diagnostics.Debug.WriteLine("j.expiryDate: " + j.expiryDate); System.Diagnostics.Debug.WriteLine("StudyGroup.Count: " + Db.Query<StudyGroup>("Select * from StudyGroup").Count()); System.Diagnostics.Debug.WriteLine("Job.Count: " + Db.Query<Job>("Select * from Job").Count()); System.Diagnostics.Debug.WriteLine("JobType.Count: " + Db.Query<JobType>("Select * from JobType").Count()); System.Diagnostics.Debug.WriteLine("Location.Count: " + Db.Query<Location>("Select * from Location").Count()); System.Diagnostics.Debug.WriteLine("Company.Count: " + Db.Query<Company>("Select * from Company").Count()); System.Diagnostics.Debug.WriteLine("CompanyJob.Count: " + Db.Query<CompanyJob>("Select * from CompanyJob").Count()); System.Diagnostics.Debug.WriteLine("JobTypeJob.Count: " + Db.Query<JobTypeJob>("Select * from JobTypeJob").Count()); System.Diagnostics.Debug.WriteLine("LocationJob.Count: " + Db.Query<LocationJob>("Select * from LocationJob").Count()); System.Diagnostics.Debug.WriteLine("StudyGroupJob.Count: " + Db.Query<StudyGroupJob>("Select * from StudyGroupJob").Count()); System.Diagnostics.Debug.WriteLine("Time for delete"); jc.DeleteAllExpiredJobs(); System.Diagnostics.Debug.WriteLine("Job.Count: " + Db.Query<Job>("Select * from Job").Count()); System.Diagnostics.Debug.WriteLine("CompanyJob.Count: " + Db.Query<CompanyJob>("Select * from CompanyJob").Count()); System.Diagnostics.Debug.WriteLine("JobTypeJob.Count: " + Db.Query<JobTypeJob>("Select * from JobTypeJob").Count()); System.Diagnostics.Debug.WriteLine("LocationJob.Count: " + Db.Query<LocationJob>("Select * from LocationJob").Count()); System.Diagnostics.Debug.WriteLine("StudyGroupJob.Count: " + Db.Query<StudyGroupJob>("Select * from StudyGroupJob").Count()); //CompanyJobs, StudyGroupJob, LocationJob og JobTypeJob. }