public async Task <IActionResult> Create([FromBody] SaveMealOrderVerificationResource mealOrderVerificationResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var OrderLists = mealOrderVerificationResource.OrderList; // if (mealOrderVerificationResource.IsClosed == true) // { // var orderVerificationDetail = mealOrderVerificationResource.MealOrderVerificationDetails; // foreach (SaveMealOrderVerificationDetailResource item in orderVerificationDetail) // { // var mealTypeId = item.MealTypeId; // var mealType = await mealtypeRepository.GetOne(mealTypeId); // var vendorId = mealType.MealVendorId; // item.VendorId = vendorId; // } // } var mealOrderVerification = mapper.Map <SaveMealOrderVerificationResource, MealOrderVerification>(mealOrderVerificationResource); var mealVerificationDetails = mealOrderVerification.MealOrderVerificationDetails; foreach (MealOrderVerificationDetail mealVerificationDetail in mealVerificationDetails) { if (mealVerificationDetail.VendorId <= 0) { return(BadRequest("Vendor id is required")); } // var mealTypeRecord = await mealtypeRepository.GetOne(mealVerificationDetail.MealTypeId); // mealVerificationDetail.VendorId = mealTypeRecord.MealVendorId; } mealOrderVerificationRepository.Add(mealOrderVerification); foreach (int item in OrderLists) { var Order = await mealOrderRepository.GetOne(item); Order.MealOrderVerificationId = mealOrderVerification.Id; } if (await unitOfWork.CompleteAsync() == false) { throw new Exception(message: "Create new order failed on save"); } mealOrderVerification = await mealOrderVerificationRepository.GetOne(mealOrderVerification.Id); var result = mapper.Map <MealOrderVerification, ViewMealOrderVerificationResource>(mealOrderVerification); return(Ok(result)); }
public async Task <IActionResult> Update(int id, [FromBody] SaveMealOrderVerificationResource mealOrderVerificationResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var mealOrderVerification = await mealOrderVerificationRepository.GetOne(id); if (mealOrderVerification == null) { return(NotFound()); } if (mealOrderVerification.IsClosed == true) { return(BadRequest("Can't edit the record since it was closed")); } var OrderLists = mealOrderVerificationResource.OrderList; mealOrderVerification = mapper.Map(mealOrderVerificationResource, mealOrderVerification); foreach (int item in OrderLists) { var Order = await mealOrderRepository.GetOne(item); Order.MealOrderVerification = mealOrderVerification; } if (await unitOfWork.CompleteAsync() == false) { throw new Exception(message: $"Updating order with id: {id} failed on save"); } mealOrderVerification = await mealOrderVerificationRepository.GetOne(mealOrderVerification.Id); var result = mapper.Map <MealOrderVerification, ViewMealOrderVerificationResource>(mealOrderVerification); return(Ok(result)); }