public ConsumerNotificationSettingsViewModel(ConsumerNotificationSetting model)
        {
            this.Id = model.Id;

            this.RepetingTypeId   = model.RepetingTypeId;
            this.RepetingTypeName = model.NotificationType.Name;
            this.StatusId         = model.StatusId;
            this.StatusName       = model.NotificationStatus.Name;
            this.Name             = model.Name;
            this.Note             = model.Note;
            this.DateStart        = model.DateStart;
            UpdatedById           = model.UpdatedById;
            if (this.UpdatedById.HasValue)
            {
                this.UpdatedByName = model.SystemUser1.LastName + ", " + model.SystemUser1.FirstName;
            }

            AddedById        = model.AddedById;
            this.AddedByName = model.SystemUser.LastName + ", " + model.SystemUser.FirstName;
            DateCreated      = model.DateCreated;
            DateUpdated      = model.DateUpdated;
            ConsumerId       = model.ConsumerId;

            if (model.ConsumerNotificationRecipients.Count != 0)
            {
                this.Recipients = ConsumerNotificationRecipientViewModel.GetList(model.ConsumerNotificationRecipients.ToList());
            }
            else
            {
                this.Recipients = new List <ViewModels.ConsumerNotificationRecipientViewModel>();
            }
        }
        public ConsumerNotificationSetting GetModel()
        {
            ConsumerNotificationSetting model = new ConsumerNotificationSetting();

            if (this.Id.HasValue)
            {
                model.Id = this.Id.Value;
            }
            else
            {
                model.Id = 0;
            }
            model.ConsumerId     = this.ConsumerId.Value;
            model.RepetingTypeId = this.RepetingTypeId;
            model.StatusId       = this.StatusId;
            model.Name           = this.Name;
            model.Note           = this.Note;
            model.DateStart      = this.DateStart;

            model.AddedById   = this.AddedById;
            model.UpdatedById = this.UpdatedById;
            model.DateCreated = this.DateCreated;
            model.DateUpdated = this.DateUpdated;


            return(model);
        }
        public async Task <Int32> Save(ConsumerNotificationSetting dbModel, List <ConsumerNotificationRecipient> recipients)
        {
            if (dbModel.Id == 0)
            {
                _context.ConsumerNotificationSettings.Add(dbModel);
            }
            else
            {
                var model = await _context.ConsumerNotificationSettings.SingleOrDefaultAsync(x => x.Id == dbModel.Id);

                if (model != null)
                {
                    model.RepetingTypeId = dbModel.RepetingTypeId;
                    model.StatusId       = dbModel.StatusId;
                    model.Name           = dbModel.Name;
                    model.Note           = dbModel.Note;
                    model.DateStart      = dbModel.DateStart;
                    model.AddedById      = dbModel.AddedById;
                    model.UpdatedById    = dbModel.UpdatedById;
                    model.DateCreated    = dbModel.DateCreated;
                    model.DateUpdated    = dbModel.DateUpdated;
                    var old = _context.ConsumerNotificationRecipients.Where(x => x.NotificationId == dbModel.Id);
                    _context.ConsumerNotificationRecipients.RemoveRange(old);
                }
            }
            await _context.SaveChangesAsync();

            if (recipients.Count > 0)
            {
                foreach (var recipient in recipients)
                {
                    recipient.NotificationId = dbModel.Id;
                    _context.ConsumerNotificationRecipients.Add(recipient);
                }
                await _context.SaveChangesAsync();
            }
            return(dbModel.Id);
        }