public async Task <CampaignData> Handle(UpdateCampaignCommand request, CancellationToken cancellationToken)
        {
            var transaction = _context.Database.BeginTransaction();

            try
            {
                var upCampaign = _context.Campaign.Include(x => x.CampaignTags).Single(s => s.Id == request.Id);

                upCampaign.EndDate = request.EndDate;

                upCampaign.IdCustomer = request.Customer.Id;

                upCampaign.IdEditor = request.Editor.Id;

                upCampaign.Description = request.Description;

                upCampaign.Title    = request.Title;
                upCampaign.Modified = DateTime.UtcNow;

                var upTags = new List <CampaignTags>();

                foreach (var item in request.Tags)
                {
                    var tag = new CampaignTags {
                        IdTags = item.Id
                    };
                    upTags.Add(tag);
                }

                _context.CampaignTags.RemoveRange(upCampaign.CampaignTags);

                upCampaign.CampaignTags = upTags;

                _context.Entry(upCampaign).State = EntityState.Modified;

                //_context.Campaign.Update(upCampaign);

                await _context.SaveChangesAsync(cancellationToken);

                var config = new MapperConfiguration(cfg => cfg.CreateMap <Campaign, CampaignData>()
                                                     .ForMember(x => x.Status, opt => opt.Ignore()));
                var mapper = config.CreateMapper();

                CampaignData model = mapper.Map <CampaignData>(upCampaign);

                ////Get Editor Name & Id
                //model.Editor = new Models.Editor();
                //model.Editor.Id = upCampaign.IdEditor;
                //model.Editor.Name = _context.Users.Find(upCampaign.IdEditor).Name;

                //Get Customer Name & Id
                model.Customer      = new Models.Customer();
                model.Customer.Id   = upCampaign.IdCustomer;
                model.Customer.Name = _context.Users.Find(upCampaign.IdCustomer).Name;

                //Get Status Name & Id
                model.Status    = new Models.Status();
                model.Status.Id = upCampaign.Status;
                var stat = _context.StatusCampaign.Find(upCampaign.Status);
                model.Status.Name  = stat.Name;
                model.Status.Color = stat.Color;

                //Get ListTag
                List <Models.Tag> ls = new List <Models.Tag>();
                foreach (var tag in upCampaign.CampaignTags)
                {
                    var cTag = new Models.Tag {
                        Id = tag.IdTags, Name = _context.Tags.Find(tag.IdTags).Name
                    };
                    ls.Add(cTag);
                }

                model.listTag = ls;
                transaction.Commit();
                return(model);
            }
            catch (Exception e)
            {
                transaction.Rollback();
                return(null);
            }
        }
Example #2
0
        private void InitTestData()
        {
            if (!Users.Any())
            {
                return;
            }
            if (!Categories.Any())
            {
                Categories.Add(new Category()
                {
                    Name = "Games"
                });
                Categories.Add(new Category()
                {
                    Name = "Films"
                });
                Categories.Add(new Category()
                {
                    Name = "Music"
                });
                Categories.Add(new Category()
                {
                    Name = "Books"
                });
                Categories.Add(new Category()
                {
                    Name = "Arts"
                });
                Categories.Add(new Category()
                {
                    Name = "Devices"
                });

                SaveChanges();
            }
            if (!CampaignTags.Any())
            {
                CampaignTags.Add(new Tag()
                {
                    Name = "shooter"
                });
                CampaignTags.Add(new Tag()
                {
                    Name = "rpg"
                });
                CampaignTags.Add(new Tag()
                {
                    Name = "comedy"
                });
                CampaignTags.Add(new Tag()
                {
                    Name = "history"
                });
                CampaignTags.Add(new Tag()
                {
                    Name = "3d"
                });
                CampaignTags.Add(new Tag()
                {
                    Name = "2d"
                });

                SaveChanges();
            }
            if (!Campaigns.Any())
            {
                var _c1 = new Campaign()
                {
                    Name           = "Shooter Campaign",
                    Description    = "Lorem ipsum .... description",
                    CreatingDate   = DateTime.Now.AddDays(-1.5f),
                    RequiredAmount = 1500.0f,

                    Creator  = Users.FirstOrDefault(x => x.Email == "*****@*****.**"),
                    Category = Categories.FirstOrDefault(x => x.Name == "Games"),
                    Tags     = new List <Tag>()
                    {
                        CampaignTags.FirstOrDefault(x => x.Name == "Shooter"),
                        CampaignTags.FirstOrDefault(x => x.Name == "3D")
                    }
                };
                var _c2 = new Campaign()
                {
                    Name           = "RPG Campaign",
                    Description    = "Lorem ipsum ... description",
                    CreatingDate   = DateTime.Now.AddDays(-0.5f),
                    RequiredAmount = 2000.0f,

                    Creator  = Users.FirstOrDefault(x => x.Email == "*****@*****.**"),
                    Category = Categories.FirstOrDefault(x => x.Name == "Games"),
                    Tags     = new List <Tag>()
                    {
                        CampaignTags.FirstOrDefault(x => x.Name == "RPG"),
                        CampaignTags.FirstOrDefault(x => x.Name == "2D")
                    }
                };

                Campaigns.Add(_c1);
                Campaigns.Add(_c2);

                SaveChanges();
            }
            if (!Comments.Any())
            {
                Comments.Add(new Comment()
                {
                    Campaign     = Campaigns.FirstOrDefault(x => x.Name == "Shooter Campaign"),
                    CreatorId    = Users.FirstOrDefault(x => x.Email == "*****@*****.**").Id,
                    CreatingDate = DateTime.Now,
                    Body         = "It's amazing. My comment)))"
                });
                Comments.Add(new Comment()
                {
                    Campaign     = Campaigns.FirstOrDefault(x => x.Name == "Shooter Campaign"),
                    CreatorId    = Users.FirstOrDefault(x => x.Email == "*****@*****.**").Id,
                    CreatingDate = DateTime.Now,
                    Body         = "My second comment!!!"
                });

                SaveChanges();
            }
        }
        public async Task <CampaignData> Handle(CreateCampaignCommand request, CancellationToken cancellationToken)
        {
            // Start a local transaction.
            var transaction = _context.Database.BeginTransaction();

            try
            {
                var Tags = new List <CampaignTags>();

                foreach (var item in request.Tags)
                {
                    var tag = new CampaignTags {
                        IdTags = item.Id, CreatedDate = DateTime.UtcNow
                    };
                    Tags.Add(tag);
                }

                var newCampaign = new Campaign
                {
                    EndDate      = request.EndDate,
                    IdCustomer   = request.Customer.Id,
                    IdEditor     = request.Editor.Id,
                    Description  = request.Description,
                    Title        = request.Title,
                    StartedDate  = DateTime.UtcNow,
                    Status       = 1,
                    IdMarketer   = request.IdMarketer,
                    CampaignTags = Tags
                };

                _context.Campaign.Add(newCampaign);

                await _context.SaveChangesAsync(cancellationToken);

                var config = new MapperConfiguration(cfg => cfg.CreateMap <Campaign, CampaignData>()
                                                     .ForMember(x => x.Status, opt => opt.Ignore()));
                var mapper = config.CreateMapper();

                CampaignData model = mapper.Map <CampaignData>(newCampaign);

                ////Get Editor Name & Id
                //model.Editor = new Models.Editor();
                //model.Editor.Id = newCampaign.IdEditor;
                //model.Editor.Name = _context.Users.Find(newCampaign.IdEditor).Name;

                //Get Customer Name & Id
                model.Customer      = new Models.Customer();
                model.Customer.Id   = newCampaign.IdCustomer;
                model.Customer.Name = _context.Users.Find(newCampaign.IdCustomer).Name;

                //Get Status Name & Id
                model.Status    = new Models.Status();
                model.Status.Id = newCampaign.Status;
                var stat = _context.StatusCampaign.Find(newCampaign.Status);
                model.Status.Name  = stat.Name;
                model.Status.Color = stat.Color;

                //Get ListTag
                List <Models.Tag> ls = new List <Models.Tag>();
                foreach (var tag in newCampaign.CampaignTags)
                {
                    var cTag = new Models.Tag {
                        Id = tag.IdTags, Name = _context.Tags.Find(tag.IdTags).Name
                    };
                    ls.Add(cTag);
                }

                model.listTag = ls;
                transaction.Commit();
                return(model);
            }catch (Exception e)
            {
                transaction.Rollback();
                return(null);
            }
        }