public async Task <int?> CreateVoucher(VoucherRequest voucherRequest) { var numOfVouchersCreated = voucherRequest.NumbersOfVoucherToCreate; //let each voucher service handle its own creation try { voucherRequest.CreationDate = DateTime.Now; voucherRequest.Metadata = Guid.NewGuid().ToString(); if (voucherRequest.VoucherType.ToUpper() == "GIFT") { Task.Run(() => _giftVoucherService.CreateGiftVoucher(voucherRequest)); } else if (voucherRequest.VoucherType.ToUpper() == "DISCOUNT") { Task.Run(() => _discountVoucherService.CreateDiscountVoucher(voucherRequest)); } else { Task.Run(() => _valueVoucherService.CreateValueVoucher(voucherRequest)); } //TODO: Log the event (VoucherCreated) var voucherGeneratedEvent = new VoucherGeneratedEvent() { EventId = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucherRequest.MerchantId, NumberGenerated = numOfVouchersCreated, Message = "New Vouchers created" }; _logger.LogInformation("Created {Number}: vouchers for {Merchant} :{@Event}", numOfVouchersCreated, voucherRequest.MerchantId, voucherGeneratedEvent); return(numOfVouchersCreated); } catch (VoucherCreateException ex) //something happened handle it //if some error occurred and not all voucher could be created log the error { //Log the error var creationError = new VoucherGenerationFailedEvent() { EventId = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucherRequest.MerchantId, FailureReason = ex.Message, Message = "Failed to generate voucher", VoucherType = voucherRequest.VoucherType, NumberToGenerate = voucherRequest.NumbersOfVoucherToCreate }; _logger.LogError("Could not generate voucher: {@creationError}", creationError); _logger.LogDebug(ex, "An error occured while creating vouchers for {Merchant}", voucherRequest.MerchantId); return(null); } }
public async Task <int?> CreateVoucher(VoucherRequest voucherRequest) { var numOfVouchersCreated = 0; //let each voucher service handle its own creation try { voucherRequest.CreationDate = DateTime.Now; if (voucherRequest.VoucherType.ToUpper() == "GIFT") { voucherRequest.CreationDate = DateTime.Now; voucherRequest.Metadata = Guid.NewGuid().ToString(); if (voucherRequest.VoucherType.ToUpper() == "GIFT") { numOfVouchersCreated += await _giftVoucherService.CreateGiftVoucher(voucherRequest); } else if (voucherRequest.VoucherType.ToUpper() == "DISCOUNT") { numOfVouchersCreated += await _discountVoucherService.CreateDiscountVoucher(voucherRequest); } else { numOfVouchersCreated += await _valueVoucherService.CreateValueVoucher(voucherRequest); } //TODO: Log the event (VoucherCreated) var voucherGeneratedEvent = new VoucherGeneratedEvent() { EventId = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucherRequest.MerchantId, NumberGenerated = numOfVouchersCreated, Message = "New Vouchers created" }; _logger.LogInformation("Created {Number}: vouchers for {Merchant} :{@Event}", numOfVouchersCreated, voucherRequest.MerchantId, voucherGeneratedEvent); return(numOfVouchersCreated); } else if (voucherRequest.VoucherType.ToUpper() == "DISCOUNT") { //TODO: Log the error event (VoucherGenerationFailed) var voucherGeneratedEvent = new VoucherGeneratedEvent() { EventId = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucherRequest.MerchantId, NumberGenerated = numOfVouchersCreated, Message = "New Vouchers created" }; _logger.LogInformation("Created {Number}: vouchers for {Merchant} :{@Event}", numOfVouchersCreated, voucherRequest.MerchantId, voucherGeneratedEvent); //handle the error here; what should happen, try again or what return(null); } else { numOfVouchersCreated += await _valueVoucherService.CreateValueVoucher(voucherRequest); } //Log the event (Voucher Created) _logger.LogInformation("Created {Number}: vouchers for {Merchant}", numOfVouchersCreated, voucherRequest.MerchantId); return(numOfVouchersCreated); } catch (VoucherCreateException ex) //something happened handle it //if some error occurred and not all voucher could be created log the error { //Log the error _logger.LogError(ex, "An error occured while creating vouchers for {Merchant}", voucherRequest.MerchantId); return(null); } }