public async Task<ActionResult> UpdateScholarshipItemAsync([FromBody] ScholarshipItem scholarshipItemToUpdate) { var scholarshipItem = await _scholarshipContext.ScholarshipItems.SingleOrDefaultAsync(i => i.Id == scholarshipItemToUpdate.Id); if (scholarshipItem == null) { return NotFound(new { Message = $"Item with id {scholarshipItemToUpdate.Id} not found." }); } var oldAmount = scholarshipItem.Amount; var raiseScholarshipItemAmountChangedEvent = oldAmount != scholarshipItemToUpdate.Amount; // Update current scholarshipItem scholarshipItem = scholarshipItemToUpdate; _scholarshipContext.ScholarshipItems.Update(scholarshipItem); if (raiseScholarshipItemAmountChangedEvent) // Save scholarshipItem's data and publish integration event through the Event Bus if Amount has changed { //Create Integration Event to be published through the Event Bus var amountChangedEvent = new ScholarshipItemAmountChangedIntegrationEvent(scholarshipItem.Id, scholarshipItemToUpdate.Amount, oldAmount); // Achieving atomicity between original Scholarship database operation and the IntegrationEventLog thanks to a local transaction await _scholarshipIntegrationEventService.SaveEventAndScholarshipContextChangesAsync(amountChangedEvent); // Publish through the Event Bus and mark the saved event as published await _scholarshipIntegrationEventService.PublishThroughEventBusAsync(amountChangedEvent); } else // Just save the updated scholarshipItem because the ScholarshipItem's Amount hasn't changed. { await _scholarshipContext.SaveChangesAsync(); } return CreatedAtAction(nameof(ItemByIdAsync), new { id = scholarshipItemToUpdate.Id }, null); }
public static void FillScholarshipItemUrl(this ScholarshipItem item, string picBaseUrl, bool azureStorageEnabled) { if (item != null) { item.PictureUri = azureStorageEnabled ? picBaseUrl + item.PictureFileName : picBaseUrl.Replace("[0]", item.Id.ToString()); } }
public async Task <IActionResult> AddToCart(ScholarshipItem scholarshipItemDetails) { try { if (scholarshipItemDetails?.Id != null) { var user = _appUserParser.Parse(HttpContext.User); await _basketSvc.AddItemToBasket(user, scholarshipItemDetails.Id); } return(RedirectToAction("Index", "Scholarship")); } catch (Exception ex) { // Catch error when Basket.api is in circuit-opened mode HandleException(ex); } return(RedirectToAction("Index", "Scholarship", new { errorMsg = ViewBag.BasketInoperativeMsg })); }
public async Task<ActionResult> CreateScholarshipItemAsync([FromBody] ScholarshipItem scholarshipItem) { var item = new ScholarshipItem { ScholarshipCurrencyId = scholarshipItem.ScholarshipCurrencyId, ScholarshipDurationId = scholarshipItem.ScholarshipDurationId, ScholarshipEducationLevelId = scholarshipItem.ScholarshipEducationLevelId, ScholarshipInterestId = scholarshipItem.ScholarshipInterestId, ScholarshipLocationId = scholarshipItem.ScholarshipLocationId, Description = scholarshipItem.Description, Name = scholarshipItem.Name, PictureFileName = scholarshipItem.PictureFileName, Amount = scholarshipItem.Amount }; _scholarshipContext.ScholarshipItems.Add(item); await _scholarshipContext.SaveChangesAsync(); return CreatedAtAction(nameof(ItemByIdAsync), new { id = item.Id }, null); }
private ScholarshipItem CreateScholarshipItem(string[] column, string[] headers, Dictionary <String, int> scholarshipCurrencyIdLookup, Dictionary <String, int> scholarshipDurationIdLookup, Dictionary <String, int> scholarshipEducationlevelIdLookup, Dictionary <String, int> scholarshipInterestIdLookup, Dictionary <String, int> scholarshipLocationIdLookup) { if (column.Count() != headers.Count()) { throw new Exception($"column count '{column.Count()}' not the same as headers count'{headers.Count()}'"); } string scholarshipCurrencyName = column[Array.IndexOf(headers, "scholarshipcurrency")].Trim('"').Trim(); if (!scholarshipCurrencyIdLookup.ContainsKey(scholarshipCurrencyName)) { throw new Exception($"currency={scholarshipCurrencyName} does not exist in scholarshipCurrencies"); } string scholarshipDurationName = column[Array.IndexOf(headers, "scholarshipduration")].Trim('"').Trim(); if (!scholarshipDurationIdLookup.ContainsKey(scholarshipDurationName)) { throw new Exception($"duration={scholarshipDurationName} does not exist in scholarshipDurations"); } string scholarshipEducationlevelName = column[Array.IndexOf(headers, "scholarshipeducationlevel")].Trim('"').Trim(); if (!scholarshipEducationlevelIdLookup.ContainsKey(scholarshipEducationlevelName)) { throw new Exception($"educationlevel={scholarshipEducationlevelName} does not exist in scholarshipEducationlevels"); } string scholarshipInterestName = column[Array.IndexOf(headers, "scholarshipinterest")].Trim('"').Trim(); if (!scholarshipInterestIdLookup.ContainsKey(scholarshipInterestName)) { throw new Exception($"interest={scholarshipInterestName} does not exist in scholarshipDurations"); } string scholarshipLocationName = column[Array.IndexOf(headers, "scholarshiplocation")].Trim('"').Trim(); if (!scholarshipLocationIdLookup.ContainsKey(scholarshipLocationName)) { throw new Exception($"location={scholarshipLocationName} does not exist in scholarshipLocations"); } string amountString = column[Array.IndexOf(headers, "amount")].Trim('"').Trim(); if (!Decimal.TryParse(amountString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out Decimal amount)) { throw new Exception($"amount={amountString}is not a valid decimal number"); } var scholarshipItem = new ScholarshipItem() { ScholarshipCurrencyId = scholarshipCurrencyIdLookup[scholarshipCurrencyName], ScholarshipDurationId = scholarshipDurationIdLookup[scholarshipDurationName], ScholarshipEducationLevelId = scholarshipEducationlevelIdLookup[scholarshipEducationlevelName], ScholarshipInterestId = scholarshipInterestIdLookup[scholarshipInterestName], ScholarshipLocationId = scholarshipLocationIdLookup[scholarshipLocationName], Description = column[Array.IndexOf(headers, "description")].Trim('"').Trim(), Name = column[Array.IndexOf(headers, "name")].Trim('"').Trim(), Amount = amount, PictureFileName = column[Array.IndexOf(headers, "picturefilename")].Trim('"').Trim(), }; int availableSlotsIndex = Array.IndexOf(headers, "availableslots"); if (availableSlotsIndex != -1) { string availableSlotsString = column[availableSlotsIndex].Trim('"').Trim(); if (!String.IsNullOrEmpty(availableSlotsString)) { if (int.TryParse(availableSlotsString, out int availableSlots)) { scholarshipItem.AvailableSlots = availableSlots; } else { throw new Exception($"availableSlots={availableSlotsString} is not a valid integer"); } } } int reslotThresholdIndex = Array.IndexOf(headers, "reslotthreshold"); if (reslotThresholdIndex != -1) { string reslotThresholdString = column[reslotThresholdIndex].Trim('"').Trim(); if (!String.IsNullOrEmpty(reslotThresholdString)) { if (int.TryParse(reslotThresholdString, out int reslotThreshold)) { scholarshipItem.ReslotThreshold = reslotThreshold; } else { throw new Exception($"reslotThreshold={reslotThreshold} is not a valid integer"); } } } int maxSlotThresholdIndex = Array.IndexOf(headers, "maxslotthreshold"); if (maxSlotThresholdIndex != -1) { string maxSlotThresholdString = column[maxSlotThresholdIndex].Trim('"').Trim(); if (!String.IsNullOrEmpty(maxSlotThresholdString)) { if (int.TryParse(maxSlotThresholdString, out int maxSlotThreshold)) { scholarshipItem.MaxSlotThreshold = maxSlotThreshold; } else { throw new Exception($"maxSlotThreshold={maxSlotThreshold} is not a valid integer"); } } } int onReapplyIndex = Array.IndexOf(headers, "onreapply"); if (onReapplyIndex != -1) { string onReapplyString = column[onReapplyIndex].Trim('"').Trim(); if (!String.IsNullOrEmpty(onReapplyString)) { if (bool.TryParse(onReapplyString, out bool onReapply)) { scholarshipItem.OnReapply = onReapply; } else { throw new Exception($"onReapply={onReapplyString} is not a valid boolean"); } } } return(scholarshipItem); }