public IHttpActionResult Delete(int id)
        {
            if (id <= 0)
            {
                return(BadRequest("Not a valid student id"));
            }

            using (var ctx = new JobSearchTrackingEntities())
            {
                var jstracking = ctx.JSTrackings
                                 .Where(jst => jst.JSTrackingID == id)
                                 .FirstOrDefault();

                if (jstracking.Company != null)
                {
                    ctx.Entry(jstracking.Company).State = System.Data.Entity.EntityState.Deleted;
                }
                if (jstracking.Contact != null)
                {
                    ctx.Entry(jstracking.Contact).State = System.Data.Entity.EntityState.Deleted;
                }
                if (jstracking.FollowUp1 != null)
                {
                    ctx.Entry(jstracking.FollowUp1).State = System.Data.Entity.EntityState.Deleted;
                }
                if (jstracking.FollowUp2 != null)
                {
                    ctx.Entry(jstracking.FollowUp2).State = System.Data.Entity.EntityState.Deleted;
                }

                jstracking.Location = null;

                ctx.Entry(jstracking).State = System.Data.Entity.EntityState.Deleted;
                ctx.SaveChanges();
            }

            return(Ok());
        }
        public IHttpActionResult Put(JSTrackingViewModel jstracking)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new JobSearchTrackingEntities())
            {
                var existingjstracking = ctx.JSTrackings.Where(s => s.JSTrackingID == jstracking.JSTrackingID)
                                         .FirstOrDefault <JSTracking>();

                Company company = jstracking.Company == null ? null : ctx.Companies.Where(c => c.Name == jstracking.Company.Name).FirstOrDefault();

                if (company == null)
                {
                    company = jstracking.Company == null ? null : new Company()
                    {
                        CompanyID = 0,
                        Name      = jstracking.Company.Name,
                        Website   = jstracking.Company.Website
                    }
                }
                ;
                else
                {
                    company.Website = jstracking.Company.Website;
                }

                Contact contact = jstracking.Contact == null ? null : ctx.Contacts.Where(c => c.Email == jstracking.Contact.EmailAddress).FirstOrDefault();

                if (contact == null)
                {
                    contact = jstracking.Contact == null ? null : new Contact()
                    {
                        ContactID   = 0,
                        Company     = company,
                        ContactName = jstracking.Contact.Name,
                        Email       = jstracking.Contact.EmailAddress,
                        Phone       = jstracking.Contact.PhoneNumber,
                    }
                }
                ;
                else
                {
                    contact.ContactName = jstracking.Contact.Name;
                    contact.Company     = company;
                    contact.Email       = jstracking.Contact.EmailAddress;
                    contact.Phone       = jstracking.Contact.PhoneNumber;
                }

                Location location = jstracking.Location == null ? null : ctx.Locations.Where(l => l.Description == jstracking.Location.Description).FirstOrDefault();

                if (location == null)
                {
                    location = jstracking.Location == null ? null : new Location()
                    {
                        LocationID  = 0,
                        Description = jstracking.Location.Description
                    }
                }
                ;
                else
                {
                    location.Description = jstracking.Location.Description;
                }

                if (existingjstracking != null)
                {
                    existingjstracking.Company   = company;
                    existingjstracking.Contact   = contact;
                    existingjstracking.FollowUp1 = (jstracking.FollowUp1 == null ||
                                                    (jstracking.FollowUp1 != null &&
                                                     jstracking.FollowUp1.Type == null &&
                                                     jstracking.FollowUp1.FollowUpDate == null)) ? null : new FollowUp1()
                    {
                        JSTracking = existingjstracking,
                        Type       = jstracking.FollowUp1.Type,
                        FollowDate = jstracking.FollowUp1.FollowUpDate
                    };

                    if (existingjstracking.FollowUp1 != null && (jstracking.FollowUp1 != null &&
                                                                 jstracking.FollowUp1.Type == null &&
                                                                 jstracking.FollowUp1.FollowUpDate == null))
                    {
                        ctx.Entry(existingjstracking.FollowUp1).State = System.Data.Entity.EntityState.Deleted;
                    }

                    existingjstracking.FollowUp2 = (jstracking.FollowUp2 == null ||
                                                    (jstracking.FollowUp2 != null &&
                                                     jstracking.FollowUp2.Type == null &&
                                                     jstracking.FollowUp2.FollowUpDate == null)) ? null : new FollowUp2()
                    {
                        JSTracking   = existingjstracking,
                        Type         = jstracking.FollowUp2.Type,
                        FollowUpDate = jstracking.FollowUp2.FollowUpDate
                    };

                    if (existingjstracking.FollowUp2 != null && (jstracking.FollowUp2 != null &&
                                                                 jstracking.FollowUp2.Type == null &&
                                                                 jstracking.FollowUp2.FollowUpDate == null))
                    {
                        ctx.Entry(existingjstracking.FollowUp2).State = System.Data.Entity.EntityState.Deleted;
                    }

                    existingjstracking.InterviewDate  = jstracking.InterviewDateTime;
                    existingjstracking.JobDescription = jstracking.JobDescription;
                    existingjstracking.Location       = location;
                    existingjstracking.Note           = jstracking.Note;
                    existingjstracking.ResumeDate     = jstracking.ResumeDate;
                    existingjstracking.SourcePosting  = jstracking.SourcePosting;

                    ctx.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok());
        }
        public IHttpActionResult PostJSTracking(JSTrackingViewModel jstracking)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data."));
            }
            using (var ctx = new JobSearchTrackingEntities())
            {
                Company company = jstracking.Company == null ? null : ctx.Companies.Where(c => c.Name == jstracking.Company.Name).FirstOrDefault();

                if (company == null)
                {
                    company = jstracking.Company == null ? null : new Company()
                    {
                        CompanyID = 0,
                        Name      = jstracking.Company.Name,
                        Website   = jstracking.Company.Website
                    }
                }
                ;
                else
                {
                    company.Website = jstracking.Company.Website;
                }

                Contact contact = jstracking.Contact == null ? null : ctx.Contacts.Where(c => c.Email == jstracking.Contact.EmailAddress).FirstOrDefault();

                if (contact == null)
                {
                    contact = jstracking.Contact == null ? null : new Contact()
                    {
                        ContactID   = 0,
                        Company     = company,
                        ContactName = jstracking.Contact.Name,
                        Email       = jstracking.Contact.EmailAddress,
                        Phone       = jstracking.Contact.PhoneNumber,
                    }
                }
                ;
                else
                {
                    contact.ContactName = jstracking.Contact.Name;
                    contact.Company     = company;
                    contact.Email       = jstracking.Contact.EmailAddress;
                    contact.Phone       = jstracking.Contact.PhoneNumber;
                }

                Location location = jstracking.Location == null ? null : ctx.Locations.Where(l => l.Description == jstracking.Location.Description).FirstOrDefault();

                if (location == null)
                {
                    location = jstracking.Location == null ? null : new Location()
                    {
                        LocationID  = 0,
                        Description = jstracking.Location.Description
                    }
                }
                ;
                else
                {
                    location.Description = jstracking.Location.Description;
                }


                ctx.JSTrackings.Add(new JSTracking()
                {
                    Company   = company,
                    Contact   = contact,
                    FollowUp1 = jstracking.FollowUp1 == null ? null : new FollowUp1()
                    {
                        Type       = jstracking.FollowUp1.Type,
                        FollowDate = jstracking.FollowUp1.FollowUpDate
                    },
                    FollowUp2 = jstracking.FollowUp2 == null ? null : new FollowUp2()
                    {
                        Type         = jstracking.FollowUp2.Type,
                        FollowUpDate = jstracking.FollowUp2.FollowUpDate
                    },
                    InterviewDate  = jstracking.InterviewDateTime,
                    JobDescription = jstracking.JobDescription,
                    Location       = location,
                    Note           = jstracking.Note,
                    ResumeDate     = jstracking.ResumeDate,
                    SourcePosting  = jstracking.SourcePosting
                });
                ctx.SaveChanges();
            }

            return(Ok());
        }