Example #1
0
        public Guid Add(PasswordInput passwordInput)
        {
            var password         = _dataProtector.Protect(passwordInput.Password);
            var numberOfAccesses = passwordInput.NumberOfAccesses == null ? null :
                                   passwordInput.NumberOfAccesses <= 1 ? 1 : passwordInput.NumberOfAccesses;
            var validFor = passwordInput.Duration;
            var entry    = new Entry(password, numberOfAccesses, validFor, _clock);

            _dbContext.Entries.Add(entry);

            if (!(passwordInput.SessionId is null))
            {
                // even if attaching fails it is still fine, we just log it
                var result = _sessionService.AttachToSession(entry, passwordInput.SessionId.Value);
                result.IfLeft(e =>
                              _logger.LogInformation(e, "Failed attaching to session: {SessionId}", passwordInput.SessionId)
                              );
            }

            _dbContext.SaveChanges();
            return(entry.Id);
        }