public async Task AddCampaign(CampaignViewModel campaignViewModel) { var campaign = campaignViewModel.Campaign; if (campaignViewModel.SelectedCampaignNetwork != null) { foreach (var network in campaignViewModel.SelectedCampaignNetwork) { if (network == "MTN") { campaign.CampaignNetwork.Add("024"); campaign.CampaignNetwork.Add("054"); campaign.CampaignNetwork.Add("055"); } if (network == "VODAFONE") { campaign.CampaignNetwork.Add("020"); campaign.CampaignNetwork.Add("050"); } if (network == "TIGO") { campaign.CampaignNetwork.Add("027"); campaign.CampaignNetwork.Add("057"); } if (network == "AIRTEL") { campaign.CampaignNetwork.Add("026"); } } } int codeQty = campaign.CampaignCodeQty; campaign.TimeCreated = DateTime.Now; campaign.CampaignStatus = true; await _dbContext.Campaigns.InsertOneAsync(campaign); string campagnId = campaign.Id.ToString(); for (int i = 0; i < codeQty; i++) { var codes = new CampaignCode(); { codes.CampaignId = campagnId; codes.CodeStatus = true; codes.TimeCreated = DateTime.Now; codes.Code = Guid.NewGuid().ToString().ToUpper().Substring(0, 11).Replace("-", string.Empty); }; await _dbContext.CampaignCodes.InsertOneAsync(codes); } }
public async Task AddCampaignCode(CodeViewModel codeViewModel) { var builder = Builders <Campaign> .Filter; var filter = builder.Eq("_id", ObjectId.Parse(codeViewModel.CampaignCode.CampaignId)); var query = _dbContext.Campaigns.Find(filter).FirstOrDefault(); if (query != null) { try { var update = Builders <Campaign> .Update.Set("CampaignCodeQty", query.CampaignCodeQty + codeViewModel.Campaign.CampaignCodeQty).CurrentDate("TimeUpdated"); await _dbContext.Campaigns.UpdateOneAsync(filter, update); int codeQty = codeViewModel.Campaign.CampaignCodeQty; codeViewModel.Campaign.TimeCreated = DateTime.Now; codeViewModel.Campaign.CampaignStatus = true; for (int i = 0; i < codeQty; i++) { var codes = new CampaignCode(); { codes.CampaignId = codeViewModel.CampaignCode.CampaignId; codes.CodeStatus = true; codes.TimeCreated = DateTime.Now; codes.Code = Guid.NewGuid().ToString().ToUpper().Substring(0, 11).Replace("-", string.Empty); }; await _dbContext.CampaignCodes.InsertOneAsync(codes); } } catch (Exception ex) { // log or manage the exception throw ex; } } }