title: Previous date and present date
body: <p>I am a bit stuck here</p>
  <p>I am hashing tokens and storing them to db and that is done in order to show user previous login date and time on the ui.</p>
  <p>Use case number : User never logged in -&gt; we should return nothing but we should save the previous date in order to show previous date value on the ui when user logins next time. So the idea is to just always return previous logged in timestamp if tokens are different</p>
  <pre class="lang-java prettyprint-override"><code>public Optional&lt;LocalDateTime&gt; test(HttpServletRequest request, UserEntity userEntity) {
      String token = extractTokenFromHeader(request);
      byte[] hashedToken = hashToken(token);
      LocalDateTime presentTime = LocalDateTime.now();
      Optional&lt;TokenEntity&gt; tokenEntity = tokenRepository.findByUserId(userEntity.getId());
  
      if (!tokenEntity.isPresent()) {
          tokenRepository.save(new TokenEntity()
                  .setToken(hashedToken)
                  .setUserId(userEntity.getId())
                  .setPresentTimeStamp(presentTime)
                  .setPreviousTimeStamp(presentTime));
          return Optional.empty();
      }
      if (!Arrays.equals(hashedToken, tokenEntity.get().getToken())) {
          tokenRepository.save(tokenEntity.get()
                  .setToken(hashedToken)
                  .setPreviousTimeStamp(tokenEntity.get().getPreviousTimeStamp())
                  .setPresentTimeStamp(presentTime));
          return tokenEntity.map(TokenEntity::getPreviousTimeStamp);
      }
      return tokenEntity.map(TokenEntity::getPreviousTimeStamp);
  }
  </code></pre>
  <p>TokenEntity has those two fields</p>
  <pre class="lang-java prettyprint-override"><code>@Column(name = &quot;previous_timestamp&quot;)
  private LocalDateTime previousTimeStamp;
  
  @Column(name = &quot;present_timestamp&quot;)
  private LocalDateTime presentTimeStamp;
  </code></pre>
  <p>Repository have custom method added which locates token entity by user id</p>
  <pre class="lang-java prettyprint-override"><code>public interface TokenRepository extends JpaRepository&lt;TokenEntity, Long&gt; {
      Optional&lt;TokenEntity&gt; findByUserId(Long id);
  }
  </code></pre>