// Path: locate-api-service/src/main/java/uk/gov/gds/locate/api/model/Usage.java
// public class Usage {
// 
//     @JsonProperty("_id")
//     private String id;
// 
//     @JsonProperty("identifier")
//     private String identifier;
// 
//     @JsonProperty("count")
//     private Integer count;
// 
//     @JsonProperty("createdAt")
//     private Date createdAt;
// 
//     @JsonProperty("expireAt")
//     private Date expireAt;
// 
//     public Usage() {
//     }
// 
//     public Usage(String id, String identifier, Integer count, Date expireAt) {
//         this.id = id;
//         this.identifier = identifier;
//         this.count = count;
//         this.expireAt = expireAt;
//         this.createdAt = new Date();
//     }
// 
//     public String getId() {
//         return id;
//     }
// 
//     public String getIdentifier() {
//         return identifier;
//     }
// 
//     public Integer getCount() {
//         return count;
//     }
// }


import com.google.common.base.Optional;
import com.mongodb.BasicDBObject;
import com.yammer.metrics.annotation.Timed;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.mongojack.DBUpdate;
import org.mongojack.JacksonDBCollection;
import uk.gov.gds.locate.api.model.Usage;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
package uk.gov.gds.locate.api.dao;



public class UsageDao {

    private final JacksonDBCollection<Usage, String> collection;

    public UsageDao(JacksonDBCollection<Usage, String> collection) {
        this.collection = collection;
    }

    @Timed
    public Optional<Usage> findUsageByIdentifier(String identifier) {
        return Optional.fromNullable(collection.findAndModify(new BasicDBObject("identifier", identifier), new DBUpdate.Builder().inc("count")));
    }

    @Timed
    public Boolean create(String identifier) {
        Date expiry = DateTime.now().withZone(DateTimeZone.UTC).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).withMillisOfSecond(999).toDate();
// Path: locate-api-service/src/main/java/uk/gov/gds/locate/api/model/Usage.java
// public class Usage {
// 
//     @JsonProperty("_id")
//     private String id;
// 
//     @JsonProperty("identifier")
//     private String identifier;
// 
//     @JsonProperty("count")
//     private Integer count;
// 
//     @JsonProperty("createdAt")
//     private Date createdAt;
// 
//     @JsonProperty("expireAt")
//     private Date expireAt;
// 
//     public Usage() {
//     }
// 
//     public Usage(String id, String identifier, Integer count, Date expireAt) {
//         this.id = id;
//         this.identifier = identifier;
//         this.count = count;
//         this.expireAt = expireAt;
//         this.createdAt = new Date();
//     }
// 
//     public String getId() {
//         return id;
//     }
// 
//     public String getIdentifier() {
//         return identifier;
//     }
// 
//     public Integer getCount() {
//         return count;
//     }
// }


// Path: locate-api-service/src/main/java/uk/gov/gds/locate/api/dao/UsageDao.java
import com.google.common.base.Optional;
import com.mongodb.BasicDBObject;
import com.yammer.metrics.annotation.Timed;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.mongojack.DBUpdate;
import org.mongojack.JacksonDBCollection;
import uk.gov.gds.locate.api.model.Usage;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

package uk.gov.gds.locate.api.dao;



public class UsageDao {

    private final JacksonDBCollection<Usage, String> collection;

    public UsageDao(JacksonDBCollection<Usage, String> collection) {
        this.collection = collection;
    }

    @Timed
    public Optional<Usage> findUsageByIdentifier(String identifier) {
        return Optional.fromNullable(collection.findAndModify(new BasicDBObject("identifier", identifier), new DBUpdate.Builder().inc("count")));
    }

    @Timed
    public Boolean create(String identifier) {
        Date expiry = DateTime.now().withZone(DateTimeZone.UTC).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).withMillisOfSecond(999).toDate();
        return collection.insert(new Usage(org.bson.types.ObjectId.get().toString(), identifier, 1, expiry)).getN() == 1;