package com.sanction.thunder.dao;

import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Expected;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Throwables;
import com.sanction.thunder.models.StormUser;

import java.io.IOException;
import java.time.Instant;
import java.util.UUID;
import javax.inject.Inject;

import static com.google.common.base.Preconditions.checkNotNull;

public class StormUsersDao {
  /** The DynamoDB table object to interact with. */
  private final Table table;

  /** A mapper for converting to and from JSON. */
  private final ObjectMapper mapper;

  @Inject
  public StormUsersDao(DynamoDB dynamo, ObjectMapper mapper) {
    this.table = dynamo.getTable("storm-users-prod");
    this.mapper = mapper;
  }

  /**
   * Insert a new StormUser into the data store.
   *
   * @param object The object to insert.
   * @return True if the object was inserted successfully, false otherwise.
   */
  public boolean insert(StormUser object) {
    checkNotNull(object);

    long now = Instant.now().toEpochMilli();
    Item item = new Item()
        .withPrimaryKey("username", object.getUsername())
        .withString("version", UUID.randomUUID().toString())
        .withLong("creation_time", now)
        .withLong("update_time", now)
        .withJSON("document", toJson(mapper, object));

    try {
      table.putItem(item, new Expected("username").notExist());
    } catch (ConditionalCheckFailedException e) {
      return false;
    }

    return true;
  }

  /**
   * Find a StormUser from the data store.
   *
   * @param username The username of the user to find.
   * @return The requested user or {@code null} if it does not exist.
   */
  public StormUser findByUsername(String username) {
    checkNotNull(username);

    Item item = table.getItem("username", username);
    if (item == null) {
      return null;
    }

    return fromJson(mapper, item.getJSON("document"));
  }

  private static String toJson(ObjectMapper mapper, StormUser object) {
    try {
      return mapper.writeValueAsString(object);
    } catch (JsonProcessingException e) {
      throw Throwables.propagate(e);
    }
  }

  private static StormUser fromJson(ObjectMapper mapper, String json) {
    try {
      return mapper.readValue(json, StormUser.class);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }

}

@@ -2250,24 +2250,960 @@
 nt%22));%0A  %7D%0A%0A
+  /**%0A   * Update a StormUser in the data store.%0A   *%0A   * @param user The user object to update. Must have the same username as the one to update.%0A   * @return True if the user was updated successfully, false otherwise.%0A   */%0A  public boolean update(StormUser user) %7B%0A    checkNotNull(user);%0A%0A    // Compute the new data%0A    long now = Instant.now().toEpochMilli();%0A    String newVersion = UUID.randomUUID().toString();%0A    String document = toJson(mapper, user);%0A%0A    // Get the old version%0A    Item item = table.getItem(%22username%22, user.getUsername());%0A    String oldVersion = item.getString(%22version%22);%0A%0A    Item newItem = item%0A        .withString(%22version%22, newVersion)%0A        .withLong(%22update_time%22, now)%0A        .withString(%22document%22, document);%0A%0A    try %7B%0A      table.putItem(newItem, new Expected(%22version%22).eq(oldVersion));%0A    %7D catch (ConditionalCheckFailedException e) %7B%0A      return false;%0A    %7D%0A%0A    return true;%0A  %7D%0A%0A
   private st

Add update method to DAO