Example #1
0
 public UpdateContactCommandHandler(IMediator mediator, IMapper mapper,
                                    IContactsDbContext context, IValidator <ContactModel> contactValidator)
 {
     _mediator         = mediator;
     _mapper           = mapper;
     _context          = context;
     _contactValidator = contactValidator;
 }
Example #2
0
 public DeleteContactCommandHandler(IContactsDbContext context)
 {
     _context = context;
 }
 public SeedContactsCommandHandler(ILogger <SeedContactsCommandHandler> logger, IMediator mediator, IContactsDbContext context)
 {
     _logger   = logger;
     _mediator = mediator;
     _context  = context;
 }
 public ContactsRepository(IContactsDbContext context)
 {
     this.DbContext = context;
 }
Example #5
0
 public HomeController(IContactsDbContext contactsDbContext)
 {
     _contactsDbContext = contactsDbContext;
 }
 public GetContactsModelQueryHandler(IContactsDbContext dbContext, IMapper mapper)
 {
     _dbContext = dbContext;
     _mapper = mapper;
 }
 public CreateContactCommandHandler(IContactsDbContext context, IValidator <ContactModel> contactValidator, IMapper mapper)
 {
     _context          = context;
     _contactValidator = contactValidator;
     _mapper           = mapper;
 }
Example #8
0
 public GetContactCountQueryHandler(IContactsDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Example #9
0
        public Entities.Contact Update(ContactEditModel model, Entities.Contact entity, IContactsDbContext dbContext)
        {
            dbContext.Entry(entity).State = EntityState.Modified;

            entity.FirstName = model.FirstName;
            entity.LastName = model.LastName;
            entity.Address = model.Address;
            entity.BirthDate = model.BirthDate;
            entity.Favorite = model.Favorite;

            #region update contact infos

            if (model.ContactInfos != null)
            {
                List<Entities.ContactInfo> contactInfos = entity.ContactInfos.ToList();
                if (contactInfos.Count > 0)
                {
                    foreach (var contactInfo in contactInfos.Where(x => model.ContactInfos.Any(y => y.IsDeleted && y.Id == x.Id)))
                    {
                        dbContext.Entry(contactInfo).State = EntityState.Deleted;
                    }
                }

                foreach (var contactInfo in model.ContactInfos.Where(x => !x.IsDeleted))
                {
                    if (contactInfo.IsNew)
                    {
                        var newContactInfo = new Entities.ContactInfo
                        {
                            Name = contactInfo.Name,
                            Type = (ContactInfoType)Enum.Parse(typeof(ContactInfoType), contactInfo.Type, true),
                            Value = contactInfo.Value
                        };
                        entity.ContactInfos.Add(newContactInfo);
                        dbContext.Entry(newContactInfo).State = EntityState.Added;
                    }
                    else
                    {
                        var oldContactInfo = entity.ContactInfos.FirstOrDefault(x => x.Id == contactInfo.Id);
                        if (oldContactInfo != null)
                        {
                            oldContactInfo.Name = contactInfo.Name;
                            oldContactInfo.Type = (ContactInfoType)Enum.Parse(typeof(ContactInfoType), contactInfo.Type, true);
                            oldContactInfo.Value = contactInfo.Value;
                        }
                        dbContext.Entry(oldContactInfo).State = EntityState.Modified;
                    }
                }
            }

            #endregion

            #region update tags

            if (model.Tags != null)
            {
                List<Entities.Tag> tags = entity.Tags.ToList();
                if (tags.Count > 0)
                {
                    foreach (var tag in tags.Where(x => model.Tags.All(y => y != x.Name)))
                    {
                        dbContext.Entry(tag).State = EntityState.Deleted;
                    }
                }

                foreach (var tag in model.Tags.Where(x => entity.Tags.All(y => y.Name != x)))
                {
                    var newTag = new Tag {Name = tag};
                    entity.Tags.Add(newTag);
                    dbContext.Entry(newTag).State = EntityState.Added;
                }
            }

            #endregion

            return entity;
        }
Example #10
0
 public ContactsController(IContactsDbContext dbContext)
 {
     _dbContext = dbContext;
 }