// DELETE api/company/delete/{id} public HttpResponseMessage Delete(int id) { using (Web_Api_Company_EmployeesEntities db = new Web_Api_Company_EmployeesEntities()) { Company DeleteModel = new Company() { ID = id }; try { db.Companies.Attach(DeleteModel); db.SaveChanges(); db.Companies.Remove(DeleteModel); db.SaveChanges(); } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "id doesn't correspond to any record in the database, therefore cannot be deleted")); } } return(Request.CreateResponse(HttpStatusCode.NoContent)); }
// POST/company/create public HttpResponseMessage Create([FromBody] Company NewCompany) { long ID_Returned = 0; //Variables to check if allowable Job titles have been provided Employee_Filter Jobs_Filter = new Employee_Filter(); List <string> Jobs_Provided = new List <string>(); string Forbidden_jobs = ""; //Check if the obtained info is not null (in this case int cannot be null (default value is 0)) if ((NewCompany != null && NewCompany.Name != "" && NewCompany.EstablishmentYear != 0) && !(NewCompany.Employees.Any(m => m.DateOfBirth == default(DateTime)))) { Forbidden_jobs = Jobs_Filter.CheckJobTitles(NewCompany.Employees); //If no forbidden jobs found proceed if (Forbidden_jobs == "") { try { using (Web_Api_Company_EmployeesEntities db = new Web_Api_Company_EmployeesEntities()) { //Save the new entry in the DB db.Companies.Add(NewCompany); db.SaveChanges(); //Get the ID of the last entry (the ID is an identity with auto incrementation) var ID = db.Companies.Max(b => b.ID); ID_Returned = ID; } } catch (Exception e) { Console.WriteLine("Error while trying to write to database, check if the SQL server is online: ", e.Message); return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Error when trying to post the content")); } return(Request.CreateResponse(HttpStatusCode.OK, ID_Returned)); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Some of the jobs provided are not allowed, (company not created): " + Forbidden_jobs)); } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Data input is null, or some data is incorrect")); } }
// PUT api/company/update/{id} public HttpResponseMessage Update(int id, [FromBody] Company NewCompany) { if (NewCompany != null) { if (!((NewCompany.Name == "" || NewCompany.EstablishmentYear == 0) || (NewCompany.Employees.Any(m => m.DateOfBirth == default(DateTime))))) { //Variables to check if allowable Job titles have been provided Employee_Filter Jobs_Filter = new Employee_Filter(); string Forbidden_jobs = ""; Forbidden_jobs = Jobs_Filter.CheckJobTitles(NewCompany.Employees); //If no forbidden jobs found proceed if (Forbidden_jobs == "") { Company UpdateComp = new Company(); using (Web_Api_Company_EmployeesEntities db = new Web_Api_Company_EmployeesEntities()) { //Retrieve the desired company to update UpdateComp = (Company)db.Companies.SingleOrDefault(z => z.ID == id); if (UpdateComp != null) { using (Web_Api_Company_EmployeesEntities db_employees = new Web_Api_Company_EmployeesEntities()) { UpdateComp.Name = NewCompany.Name; UpdateComp.EstablishmentYear = NewCompany.EstablishmentYear; //Delete all previous employees foreach (Employee item in db_employees.Employees) { if (item.CompanyToken == id) { db_employees.Employees.Remove(item); } } db_employees.SaveChanges(); //Get updated staff UpdateComp.Employees = NewCompany.Employees; db.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.NoContent)); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The company with id: " + id + "doesn't exist any longer")); } } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Some of the jobs provided are not allowed, (company not updated): " + Forbidden_jobs)); } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Company name and/or establishment year is null or data in employees is incorrect ")); } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Data input is null")); } }