private async Task<Guid> getJobId(AddUserJobBindingModel job) { var existJob = await this.Data.Jobs.All().FirstOrDefaultAsync(j => j.Name == job.Name && j.Company.Name == job.CompanyName); Guid newJobId; if (existJob == null) { var existCompany = await this.Data.Companies.All().FirstOrDefaultAsync(c => c.Name == job.CompanyName); Guid newCompanyId; if (existCompany == null) { var newCompany = new Company() { Name = job.CompanyName, CreatedOn = DateTime.Now }; this.Data.Companies.Add(newCompany); await this.Data.SaveChangesAsync(); newCompanyId = newCompany.Id; } else { newCompanyId = existCompany.Id; } var newJob = new Job() { Name = job.Name, CompanyId = newCompanyId }; this.Data.Jobs.Add(newJob); await this.Data.SaveChangesAsync(); newJobId = newJob.Id; } else { newJobId = existJob.Id; } return newJobId; }
public async Task<IHttpActionResult> PostCompany(CompanyBindingModel model) { var userId = this.User.Identity.GetUserId(); if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (model == null) { return this.BadRequest("Invalid input data"); } if (userId == null) { return this.BadRequest("Invalid session token."); } var company = new Company() { Name = model.Name, CreatedOn = DateTime.Now, Email = model.Email, OwnerId = userId }; this.Data.Companies.Add(company); await this.Data.SaveChangesAsync(); return this.StatusCode(HttpStatusCode.Created); }