public async Task <ActionResult> SupportMedicine(InsuranceSupportModel model) { try { if (!ModelState.IsValid) { TempData["message"] = ModelState.ErrorGathering(); } await _insuranceSupportService.SupportMedicineAsync(model); } catch (Exception ex) { TempData["message"] = ex.Message; } return(RedirectToAction(nameof(Index), new { id = model.Insurance })); }
public async Task <ActionResult> SupportMedicine(int id) { var insurance = await _context.Insurances .Include(x => x.Supports) .FirstOrDefaultAsync(x => x.Id.Equals(id)); if (insurance == null) { TempData["message"] = "Insurance not found"; return(RedirectToAction(nameof(Index), new { id })); } InsuranceSupportModel model = new InsuranceSupportModel(); model.Insurance = insurance.Id; model.InsuranceName = insurance.Name; return(View(model)); }
public async Task SupportMedicineAsync(InsuranceSupportModel model) { try { var insurance = await _context.Insurances.FindAsync(model.Insurance); if (insurance == null) { throw new Exception("Insurance not found"); } var medicine = await _context.Medicines.FindAsync(model.Medicine); if (medicine == null) { throw new Exception("Medicine not found"); } var alreadySupport = await _context.InsuranceSupports .FirstOrDefaultAsync(x => x.MedicineId.Equals(medicine.Id) && x.InsuranceId.Equals(insurance.Id)); if (alreadySupport != null) { throw new Exception("Medicine is already support."); } await _context.InsuranceSupports.AddAsync(new Data.Entities.InsuranceSupport() { Insurance = insurance, Medicine = medicine, InsuranceId = insurance.Id, MedicineId = insurance.Id }); await _context.SaveChangesAsync(); } catch (Exception ex) { throw ex; } }