public HttpResponseMessage Post([FromBody] CarModel newCar) { try { if (ModelState.IsValid) { CarModel addedCar = carsManager.AddCar(newCar); if (addedCar != null) { return(Request.CreateResponse(HttpStatusCode.OK, true)); } } var errorMessage = ModelState.Values.SelectMany(v => v.Errors).ToList().Select(e => e.ErrorMessage).Where(e => e != "").FirstOrDefault(); if (errorMessage == null) { errorMessage = ModelState.Values.SelectMany(v => v.Errors).ToList().Select(e => e.Exception).Where(e => e.Message != "").Select(e => e.Message).FirstOrDefault(); } if (errorMessage != null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage)); } return(Request.CreateErrorResponse(HttpStatusCode.NotFound, new HttpError("Problem in user data"))); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
static void CarsMenu() { int option = 0; while (option != 4) { Console.Clear(); Console.WriteLine("CARS MENU"); Console.WriteLine("1. List All Cars"); Console.WriteLine("2. Add a Car"); Console.WriteLine("3. Remove a Car"); Console.WriteLine("4. Back to main menu"); Console.WriteLine("Write the number of the actinon you want to take:"); try { option = Convert.ToInt32(Console.ReadLine()); switch (option) { case 1: carsManager.ShowCars(); WaitForUserInput(); break; case 2: carsManager.AddCar(); WaitForUserInput(); break; case 3: carsManager.RemoveCar(); WaitForUserInput(); break; case 4: break; default: option = 0; break; } } catch (FormatException) { Console.WriteLine("Please enter a valid option (a number from 1 to 4."); option = 0; } } }