public UnitOfWork(AppDbContext context) { _context = context; RestaurantRepository = new RestaurantRepository(context); MealCategoryRepository = new MealCategoryRepository(context); MealTypeRepository = new MealTypeRepository(context); MealRepository = new MealRepository(context); RestaurantCategoryRepository = new RestaurantCategoryRepository(context); AreaRepository = new AreaRepository(context); }
// POST api/<controller> public HttpResponseMessage Post([FromBody] PurchasedMeal value) { try { Dictionary <int, MealType> allMealTypes = MealTypeRepository.GetDictionary(); // Find the selected meal type if (allMealTypes.ContainsKey(value.MealType)) { MealType selectedMealType = allMealTypes[value.MealType]; if (value.Amount <= selectedMealType.FullAmount) { if (value.Amount >= selectedMealType.FullAmount * -1) { value = Repository.Add(value); // Apparently we should be responding to a POST request with HTTP status 201 instead of 200, which would be the default HttpResponseMessage response = Request.CreateResponse <PurchasedMeal>(HttpStatusCode.Created, value); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new Exception("Amount cannot be less than full price * -1"))); } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new Exception("Amount cannot be more than full price"))); } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new Exception("Invalid MealID"))); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
private Task <Option <MealType, Error> > ValidateMealTypeAsync(string typeId) => MealTypeRepository .GetByIdAsync(typeId) .SomeNotNull(Error.Validation($"Invalid meal type ID: {typeId}."));