public IActionResult UpdateCampaignByProduct(CampaignByProductCommand campaignCommand) { try { var result = _context.Campaigns.FirstOrDefault(x => x.Id == campaignCommand.Id); if (result is null) { ErrorMessage.Message = "Not Found Item"; return(BadRequest(ErrorMessage)); } else if (campaignCommand.ListCampaignProduct.Count == 0) { ErrorMessage.Message = "Please input ProductList"; return(BadRequest(ErrorMessage)); } var campaign = result; campaign.Id = campaignCommand.Id; campaign.PromotionName = campaignCommand.PromotionName; campaign.PromotionDetail = campaignCommand.PromotionDetail; campaign.StartDate = campaignCommand.StartDate; campaign.EndDate = campaignCommand.EndDate; campaign.UpdateDate = DateTime.Now; campaign.PromotionType = campaignCommand.PromotionType; campaign.Discount = campaignCommand.Discount; campaign.ShopId = campaignCommand.ShopId; campaign.IsDelete = false; _context.Entry(campaign).State = EntityState.Modified; _context.SaveChangesAsync(); List <CampaignDetailByProduct> campaignDetailByProduct = campaignCommand.ListCampaignProduct.Select(x => new CampaignDetailByProduct { ProductId = x.ProductId, UpdateDate = DateTime.Now, CampaignsId = campaign.Id, IsDelete = x.IsDelete }).ToList(); foreach (var item in campaignDetailByProduct) { _context.Entry(item).State = EntityState.Modified; } _context.SaveChangesAsync(); SucessMeassage.Message = "Sucess"; return(Ok(SucessMeassage)); } catch (Exception ex) { ErrorMessage.Message = ex.Message; return(StatusCode((int)HttpStatusCode.InternalServerError, ErrorMessage)); } }
public IActionResult PostCampaign(CampaignByProductCommand campaignCommand) { try { if (campaignCommand is null) { ErrorMessage.Message = "Please input data"; return(BadRequest(ErrorMessage)); } else if (campaignCommand.ListCampaignProduct.Count == 0) { ErrorMessage.Message = "Please input ProductList"; return(BadRequest(ErrorMessage)); } Campaign campaign = new Campaign(); campaign.PromotionName = campaignCommand.PromotionName; campaign.PromotionDetail = campaignCommand.PromotionDetail; campaign.StartDate = campaignCommand.StartDate; campaign.EndDate = campaignCommand.EndDate; campaign.CreatedDate = DateTime.Now; campaign.PromotionType = campaignCommand.PromotionType; campaign.Discount = campaignCommand.Discount; campaign.ShopId = campaignCommand.ShopId; campaign.IsDelete = false; _context.Campaigns.Add(campaign); _context.SaveChangesAsync(); List <CampaignDetailByProduct> campaignDetailByProduct = campaignCommand.ListCampaignProduct.Select(x => new CampaignDetailByProduct { ProductId = campaign.Id, CreatedDate = DateTime.Now, IsDelete = false, CampaignsId = campaign.Id, }).ToList(); foreach (var item in campaignDetailByProduct) { _context.CampaignDetailByProduct.Add(item); } _context.SaveChangesAsync(); SucessMeassage.Message = "Sucess"; return(Ok(SucessMeassage)); } catch (Exception ex) { ErrorMessage.Message = ex.Message; return(StatusCode((int)HttpStatusCode.InternalServerError, ErrorMessage)); } }