Ejemplo n.º 1
0
        // basic sync for db items  .. NOTE uses dummy items at the moment for debugging
        public async void SyncAPiToDB()
        {
            // get db connection
            DbAccessor db = new DbAccessor();

            db.CreateTable <Models.Company.MyCompany>();

            // company users
            var usersdb = db.GetTableItems <CompanyUser>();

            if (usersdb != null && usersdb.Count == 0)
            {
                var prgApi = DummyEmpData.GetCompanyUsers(); // store in db as CompanyUserDetails in json
                foreach (var pg in prgApi)
                {
                    CompanyUser item = new CompanyUser {
                        UserId = pg.UserId, UserRole = pg.AssignedRole, JSON = JsonConvert.SerializeObject(pg)
                    };
                    db.InsertRecord <CompanyUser>(item);
                }
            }

            // company details
            var companydb = db.GetTableItems <CompanyProfile>();

            if (companydb != null && companydb.Count == 0)
            {
                var            comp = DummyEmpData.GetCompanyDetails(); // store in db as CompanyProfile in json
                CompanyProfile item = new CompanyProfile {
                    ProfileId = comp.CompanyId, JSON = JsonConvert.SerializeObject(comp)
                };
                db.InsertRecord <CompanyProfile>(item);
            }

            // employer posted jobs
            var empdb = db.GetTableItems <EmployerJobs>();

            if (empdb != null && empdb.Count == 0)
            {
                var prgApi = DummyEmpData.GetEmployerPostedJobs(); // store in db as EmployerJobs in json
                foreach (var pg in prgApi)
                {
                    var item = new EmployerJobs  {
                        JobPostId = pg.JobPostId, Status = pg.Status, JSON = JsonConvert.SerializeObject(pg)
                    };
                    db.InsertRecord <EmployerJobs>(item);
                }
            }

            // current assigned programs for company
            var myProgdb = db.GetTableItems <MyPrograms>();

            if (myProgdb != null && myProgdb.Count == 0)
            {
                var prgApi = DummyEmpData.GetCurrentPrograms(); // store in db as EmployerProgram in json
                foreach (var pg in prgApi)
                {
                    var item = new MyPrograms {
                        ProgramId = pg.ProgramId, JSON = JsonConvert.SerializeObject(pg)
                    };
                    db.InsertRecord <MyPrograms>(item);
                }
            }

            // add all programs
            var allProgdb = db.GetTableItems <DBAccess.Programs>();

            if (allProgdb != null && allProgdb.Count == 0)
            {
                var prgApi = DummyEmpData.GetAllPrograms(); // store in db as EmployerProgram in json
                foreach (var pg in prgApi)
                {
                    var item = new DBAccess.Programs {
                        ProgramId = pg.ProgramId, JSON = JsonConvert.SerializeObject(pg)
                    };
                    db.InsertRecord <DBAccess.Programs>(item);
                }
            }

            // current assigned employer services // save as EmployerService
            var servdb = db.GetTableItems <MyServices>();

            if (servdb != null && servdb.Count == 0)
            {
                var prgApi = DummyEmpData.GetEmployerServices(); // store in db as MyServices in json
                foreach (var pg in prgApi)
                {
                    var item = new MyServices {
                        ServiceId = pg.ServiceId, JSON = JsonConvert.SerializeObject(pg)
                    };
                    db.InsertRecord <MyServices>(item);
                }
            }

            // all services
            var allservdb = db.GetTableItems <AllServices>();

            if (allservdb != null && allservdb.Count == 0)
            {
                var prgApi = DummyEmpData.GetALLServices(); // store in db as AllServices in json
                foreach (var pg in prgApi)
                {
                    var item = new AllServices {
                        ServiceId = pg.ServiceId, JSON = JsonConvert.SerializeObject(pg)
                    };
                    db.InsertRecord <AllServices>(item);
                }
            }

            // announcements
            var anndb = db.GetTableItems <Announcement>();

            if (anndb != null && anndb.Count == 0)
            {
                var prgApi = DummyEmpData.GetAnnouncements();
                foreach (var pg in prgApi)
                {
                    try
                    {
                        db.InsertRecord <Announcement>(new Announcement
                        {
                            CompanyName = pg.CompanyName,
                            Description = pg.Description,
                            Featured    = false,
                            Image       = "",
                            Ttle        = pg.Ttle,
                            Url         = pg.Url
                        });
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("ERROR:" + ex.Message);
                    }
                }
            }

            // job applicants list
            var jobappdb = db.GetTableItems <JobApplicant>();

            if (jobappdb != null && jobappdb.Count == 0)
            {
                var prgApi = DummyEmpData.GetCompanyJobApplications(); // store ApplicationProfile as json
                foreach (var pg in prgApi)
                {
                    var item = new JobApplicant
                    {
                        ApplicantId = pg.ApplicationId.ToString(), JobPostId = pg.JobPostId.ToString(), JSON = JsonConvert.SerializeObject(pg)
                    };
                    db.InsertRecord <JobApplicant>(item);
                }
            }

            // search applicants
            var matcheddb = db.GetTableItems <MatchedClients>();

            if (matcheddb != null && matcheddb.Count == 0)
            {
                var prgApi = DummyEmpData.GetCompanyJobApplications(); // store ApplicationProfile as json
                foreach (var pg in prgApi)
                {
                    var item = new MatchedClients
                    {
                        ProfileId = pg.ApplicationId.ToString(),
                        JobPostId = pg.JobPostId.ToString(),
                        JSON      = JsonConvert.SerializeObject(pg)
                    };
                    db.InsertRecord <MatchedClients>(item);
                }
            }

            // event data we need
            // add in any events from dummy data
            var eventsDb = db.GetTableItems <EventDetails>();

            if (eventsDb != null && eventsDb.Count == 0)
            {
                var eventList = DummyEmpData.GetEventsList();
                foreach (var itemEv in eventList)
                {
                    // new item so add it in :)
                    EventDetails item = new EventDetails
                    {
                        EventId        = itemEv.EventId,
                        EventEnd       = itemEv.EventEnd,
                        EventLocation  = itemEv.EventLocation,
                        EventStart     = itemEv.EventStart,
                        EventTitle     = itemEv.EventTitle,
                        Status         = itemEv.Status,
                        AdditionalInfo = itemEv.AdditionalInfo,
                        Email          = itemEv.Email,
                        PhoneNumber    = itemEv.PhoneNumber,
                        JobPostId      = itemEv.JobPostId
                    };

                    db.InsertRecord <EventDetails>(item);
                }
            }

            // insert dummy compaints data
            var ComplaintsDb = db.GetTableItems <ComplaintRaised>();

            if (ComplaintsDb != null && ComplaintsDb.Count == 0)
            {
                // fill with new data
                var compaintsData = DummyEmpData.GetComplaintsList();
                foreach (var itemEv in compaintsData)
                {
                    db.InsertRecord <ComplaintRaised>(new ComplaintRaised
                    {
                        ComplaintText = itemEv.ComplaintText,
                        ComplaintId   = itemEv.ComplaintId,
                        Subject       = itemEv.Subject,
                        Status        = itemEv.Status,
                        CreatedOn     = itemEv.CreatedOn,
                        ClosedOn      = itemEv.ClosedOn,
                        Category      = itemEv.Category
                    });
                }
            }
        }