public IHttpActionResult PostDriver([FromUri] string senderID, [FromBody] DriverModel driverModel) { Driver driver = new Driver(driverModel.Username, driverModel.Password) { FirstName = driverModel.FirstName, LastName = driverModel.LastName, Gender = driverModel.Gender, JMBG = driverModel.JMBG, Phone = driverModel.Phone, Email = driverModel.Email, DriversLocation = DbLocation.GetSingleEntityByKey(driverModel.DriversLocationID), DriversVehicle = DbVehicle.GetSingleEntityByKey(driverModel.DriversVehicleID), }; driverModel.TaxiDrivesIDs.ForEach(td => driver.TaxiDrives.Add(DbTaxiDrive.GetSingleEntityByKey(td))); if (!LoggedUsers.Contains(senderID)) { return(Content(HttpStatusCode.Unauthorized, "Not logged in.")); } else if (!DbAdmin.Exists(senderID)) { return(Content(HttpStatusCode.Unauthorized, "Not a dispatcher.")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } bool result; try { result = DbDriver.Add(driver); } catch (Exception e) { Trace.Write($"Error on 'PostDriver()'. Error message: {e.Message}"); Trace.Write($"[STACK_TRACE] {e.StackTrace}"); return(InternalServerError(e)); } if (result) { return(Ok(driver)); } else { return(BadRequest("Driver already exists.")); } }
// PUT api/drivers/5 public IHttpActionResult PutDriver([FromUri] string senderID, [FromBody] DriverModel driverModel) { bool result = false; if (!LoggedUsers.Contains(senderID)) { return(Content(HttpStatusCode.Unauthorized, "Not logged in.")); } if (DbDriver.Exists(driverModel.Username)) { if (!DbAdmin.Exists(senderID) || senderID != driverModel.Username) { return(Content(HttpStatusCode.Unauthorized, "Not a dispatcher nor a user to be modified.")); } try { Driver driver = new Driver(driverModel.Username, driverModel.Password) { FirstName = driverModel.FirstName, LastName = driverModel.LastName, Gender = driverModel.Gender, JMBG = driverModel.JMBG, Phone = driverModel.Phone, Email = driverModel.Email, DriversLocation = DbLocation.GetSingleEntityByKey(driverModel.DriversLocationID), DriversVehicle = DbVehicle.GetSingleEntityByKey(driverModel.DriversVehicleID), }; driverModel.TaxiDrivesIDs.ForEach(td => driver.TaxiDrives.Add(DbTaxiDrive.GetSingleEntityByKey(td))); result = DbDriver.Modify(driver); } catch (Exception e) { Trace.Write($"Error on 'PutDriver()'. Error message: {e.Message}"); Trace.Write($"[STACK_TRACE] {e.StackTrace}"); return(InternalServerError(e)); } } if (result) { return(Ok(driverModel)); } else { return(NotFound()); } }