/Heroku - mLab MongoDB - Spring Boot - How to start example StatEntry.java
Last active Jun 5, 2019
Heroku - mLab MongoDB - Spring Boot - How to start example
# MONGODB | |
spring.data.mongodb.uri=${MONGODB_URI} |
import org.springframework.data.annotation.Id; | |
import org.springframework.data.mongodb.core.mapping.Document; | |
@Document(collection = "stats") | |
public class StatEntry { | |
@Id | |
public String id; | |
private long time; | |
private String referrer; | |
private String site; | |
... | |
} |
import org.springframework.data.mongodb.repository.MongoRepository; | |
import java.util.List; | |
public interface StatEntryRepo extends MongoRepository<StatEntry, String> { | |
StatEntry findOneById(String id); | |
List<StatEntry> findAll(); | |
void delete(StatEntry entry); | |
void deleteAll(); | |
} |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
import java.time.Instant; | |
import java.util.UUID; | |
@Component | |
public class StatService { | |
@Autowired | |
public StatEntryRepo statsRepo; | |
public void addStatEntry(String site, String referrer) { | |
StatEntry entry = new StatEntry(); | |
entry.setSite(site); | |
entry.setReferrer(referrer); | |
entry.setTime(Instant.now().toEpochMilli()); | |
entry.setId(UUID.randomUUID().toString()); | |
statsRepo.save(entry); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment