public async Task <IActionResult> RegisterItem(AddItemModelView itemToAdd) { if (!ModelState.IsValid || itemToAdd == null) { return(BadRequest(new ErrorModelView("information not accepted"))); } try { var request = new HttpRequestMessage(HttpMethod.Get, $"meals/{itemToAdd.mealId}"); var client = clientFactory.CreateClient("gfmm"); var clientResponse = await client.SendAsync(request); if (clientResponse.StatusCode == HttpStatusCode.NotFound) { return(BadRequest(new ErrorModelView($"no meal was found by the resource identifier '{itemToAdd.mealId}'"))); } var responsePayload = await clientResponse.Content.ReadAsStreamAsync(); JsonSerializerOptions deserializationOptions = new JsonSerializerOptions(); deserializationOptions.PropertyNameCaseInsensitive = true; GetDetailedMealInformationModelView meal = await JsonSerializer.DeserializeAsync <GetDetailedMealInformationModelView>(responsePayload, deserializationOptions); DateTime expirationDate = DateTime.Parse(itemToAdd.expirationDate); DateTime productionDate = DateTime.Parse(itemToAdd.productionDate); Item item = new Item(MealID.ValueOf(meal.Designation), productionDate, expirationDate); this.repositoryFactory.ItemRepository().Save(item); RegisterItemModelView response = new RegisterItemModelView(item); return(StatusCode(201, response)); } catch (InvalidOperationException argumentException) { // TODO: Log exception return(BadRequest(new ErrorModelView(argumentException.Message))); } catch (ArgumentException argumentException) { // TODO: Log exception return(BadRequest(new ErrorModelView(argumentException.Message))); } catch (FormatException dateFormatException) { // TODO: Log exception return(BadRequest(new ErrorModelView(dateFormatException.Message))); } catch (Exception databaseException) { // TODO: Log exception return(StatusCode(500, null)); } }
public async Task <IActionResult> AvailableItems([FromQuery] long mealId) { try { List <Item> items = null; if (mealId == 0) { items = repositoryFactory.ItemRepository().All(); } else { var request = new HttpRequestMessage(HttpMethod.Get, $"meals/{mealId}"); var client = clientFactory.CreateClient("gfmm"); var clientResponse = await client.SendAsync(request); if (clientResponse.StatusCode == HttpStatusCode.NotFound) { return(NotFound(null)); } var responsePayload = await clientResponse.Content.ReadAsStreamAsync();; JsonSerializerOptions deserializationOptions = new JsonSerializerOptions(); deserializationOptions.PropertyNameCaseInsensitive = true; GetDetailedMealInformationModelView meal = await JsonSerializer.DeserializeAsync <GetDetailedMealInformationModelView>(responsePayload, deserializationOptions); MealID mealIdVO = MealID.ValueOf(meal.Designation); items = repositoryFactory.ItemRepository().All().Where((item) => item.MealId.Equals(mealIdVO)).ToList(); } if (items.Count == 0) { return(NotFound(null)); } GetAvailableItemsModelView modelview = new GetAvailableItemsModelView(items); return(Ok(modelview)); } catch (Exception databaseException) { // TODO: Log exception return(StatusCode(500, null)); } }
public IActionResult CreateMeal([FromBody] CreateMealModelView createModelView) { try { string designation = createModelView.Designation; MealType type = MealType.ValueOf(createModelView.Type); List <Ingredient> ingredients = createModelView.Ingredients.Select((ingredient) => Ingredient.ValueOf(ingredient)).ToList(); List <Allergen> allergens = createModelView.Allergens.Select((allergen) => Allergen.ValueOf(allergen)).ToList(); List <Descriptor> descriptors = createModelView.Descriptors.Select((descriptor) => Descriptor.ValueOf(descriptor.Name, descriptor.Quantity, descriptor.QuantityUnit)).ToList(); Meal meal; if (allergens.Count == 0) { meal = new Meal(designation, type, ingredients, descriptors); } else { meal = new Meal(designation, type, ingredients, descriptors, allergens); } meal = factory.MealRepository().Save(meal); GetDetailedMealInformationModelView modelview = new GetDetailedMealInformationModelView(meal); return(Created("api/meals", modelview)); } catch (InvalidOperationException argumentException) { // TODO: Log exception return(BadRequest(new ErrorModelView(argumentException.Message))); } catch (ArgumentException argumentException) { // TODO: Log exception return(BadRequest(new ErrorModelView(argumentException.Message))); } catch (Exception databaseException) { // TODO: Log exception return(StatusCode(500, null)); } }
public IActionResult DetailedMealInformation(long id) { try { Meal meal = factory.MealRepository().Find(id); GetDetailedMealInformationModelView modelview = new GetDetailedMealInformationModelView(meal); return(Ok(modelview)); } catch (ArgumentException noMealMatchIdException) { // TODO: Log exception return(NotFound(null)); } catch (Exception databaseException) { // TODO: Log exception return(StatusCode(500, null)); } }