Skip to content
This repository was archived by the owner on Dec 11, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
out

### STS ###
.apt_generated
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/scmspain/MsFcTechTestApplication.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import com.scmspain.configuration.InfrastructureConfiguration;
import com.scmspain.configuration.TweetConfiguration;
import com.scmspain.configuration.ValidatorConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@EnableAutoConfiguration
@Import({TweetConfiguration.class, InfrastructureConfiguration.class})
@Import({TweetConfiguration.class, InfrastructureConfiguration.class, ValidatorConfiguration.class})
public class MsFcTechTestApplication {

public static void main(String[] args) {
SpringApplication.run(MsFcTechTestApplication.class, args);
}

}
5 changes: 4 additions & 1 deletion src/main/java/com/scmspain/configuration/InfrastructureConfiguration.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

@Configuration
public class InfrastructureConfiguration {
@Bean @ExportMetricWriter

@Bean
@ExportMetricWriter
public MetricWriter getMetricWriter(MBeanExporter exporter) {
return new JmxMetricWriter(exporter);
}

}
17 changes: 13 additions & 4 deletions src/main/java/com/scmspain/configuration/TweetConfiguration.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.scmspain.configuration;

import com.scmspain.controller.TweetController;
import com.scmspain.repository.ITweetRepository;
import com.scmspain.repository.TweetRepository;
import com.scmspain.services.TweetService;
import com.scmspain.validators.TweetValidator;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -10,13 +13,19 @@

@Configuration
public class TweetConfiguration {
@Bean
public TweetService getTweetService(EntityManager entityManager, MetricWriter metricWriter) {
return new TweetService(entityManager, metricWriter);
}

@Bean
public TweetController getTweetConfiguration(TweetService tweetService) {
return new TweetController(tweetService);
}

@Bean
public TweetRepository getTweetRepository(EntityManager entityManager) {
return new TweetRepository(entityManager);
}

@Bean
public TweetService getTweetService(ITweetRepository tweetRepository, MetricWriter metricWriter, TweetValidator tweetValidator) {
return new TweetService(tweetRepository, metricWriter, tweetValidator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.scmspain.configuration;

import com.scmspain.validators.TweetValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ValidatorConfiguration {

@Bean
public TweetValidator tweetValidator() {
return new TweetValidator();
}
}
36 changes: 29 additions & 7 deletions src/main/java/com/scmspain/controller/TweetController.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.scmspain.controller;

import com.scmspain.controller.command.PublishTweetCommand;
import com.scmspain.controller.command.TweetCommand;
import com.scmspain.entities.Tweet;
import com.scmspain.exception.NotFoundException;
import com.scmspain.services.TweetService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.*;

@RestController
public class TweetController {

private TweetService tweetService;

public TweetController(TweetService tweetService) {
Expand All @@ -20,13 +21,24 @@ public TweetController(TweetService tweetService) {

@GetMapping("/tweet")
public List<Tweet> listAllTweets() {
return this.tweetService.listAllTweets();
return this.tweetService.listAllPublishedTweets();
}

@PostMapping("/tweet")
@PostMapping(path = "/tweet", consumes = "application/json")
@ResponseStatus(CREATED)
public void publishTweet(@RequestBody PublishTweetCommand publishTweetCommand) {
this.tweetService.publishTweet(publishTweetCommand.getPublisher(), publishTweetCommand.getTweet());
public void publishTweet(@RequestBody TweetCommand tweetCommand) {
this.tweetService.publishTweet(tweetCommand.getPublisher(), tweetCommand.getTweet());
}

@PostMapping("/discarded")
@ResponseStatus(NO_CONTENT)
public void discardTweet(@RequestBody TweetCommand tweetCommand) throws NotFoundException {
this.tweetService.discardTweet(tweetCommand.getTweet());
}

@GetMapping("/discarded")
public List<Tweet> listDiscardedTweets() {
return this.tweetService.listAllDiscardedTweets();
}

@ExceptionHandler(IllegalArgumentException.class)
Expand All @@ -38,4 +50,14 @@ public Object invalidArgumentException(IllegalArgumentException ex) {
public String exceptionClass = ex.getClass().getSimpleName();
};
}

@ExceptionHandler(NotFoundException.class)
@ResponseStatus(NOT_FOUND)
@ResponseBody
public Object notFoundException(NotFoundException ex) {
return new Object() {
public String message = ex.getMessage();
public String exceptionClass = ex.getClass().getSimpleName();
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.scmspain.controller.command;

public class PublishTweetCommand {
public class TweetCommand {

private String publisher;
private String tweet;

Expand Down
85 changes: 83 additions & 2 deletions src/main/java/com/scmspain/entities/Tweet.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
package com.scmspain.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
import java.util.Objects;

@Entity
public class Tweet {

@Id
@GeneratedValue
private Long id;

@Column(nullable = false)
private String publisher;
@Column(nullable = false, length = 140)

@Column(nullable = false, length = 2048)
private String tweet;
@Column (nullable=true)

@Column
private Long pre2015MigrationStatus = 0L;

@JsonIgnore
@Column(nullable = false)
private Date publicationDate;

@JsonIgnore
@Column(nullable = false)
private Boolean discarded = Boolean.FALSE;

@JsonIgnore
@Column
private Date discardedDate;

public Tweet() {
}

public Tweet(String publisher, String text) {
this.publisher = publisher;
this.tweet = text;
this.publicationDate = new Date();
}

public Long getId() {
return id;
}
Expand Down Expand Up @@ -52,4 +78,59 @@ public void setPre2015MigrationStatus(Long pre2015MigrationStatus) {
this.pre2015MigrationStatus = pre2015MigrationStatus;
}

public Date getPublicationDate() {
return publicationDate;
}

public void setPublicationDate(Date publicationDate) {
this.publicationDate = publicationDate;
}

public Boolean isDiscarded() {
return discarded;
}

public void setDiscarded(Boolean discarded) {
this.discarded = discarded;
}

public Date getDiscardedDate() {
return discardedDate;
}

public void setDiscardedDate(Date discardedDate) {
this.discardedDate = discardedDate;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tweet tweet1 = (Tweet) o;
return Objects.equals(id, tweet1.id) &&
Objects.equals(publisher, tweet1.publisher) &&
Objects.equals(tweet, tweet1.tweet) &&
Objects.equals(pre2015MigrationStatus, tweet1.pre2015MigrationStatus) &&
Objects.equals(publicationDate, tweet1.publicationDate) &&
Objects.equals(discarded, tweet1.discarded) &&
Objects.equals(discardedDate, tweet1.discardedDate);
}

@Override
public int hashCode() {
return Objects.hash(id, publisher, tweet, pre2015MigrationStatus, publicationDate, discarded, discardedDate);
}

@Override
public String toString() {
return "Tweet{" +
"id=" + id +
", publisher='" + publisher + '\'' +
", tweet='" + tweet + '\'' +
", pre2015MigrationStatus=" + pre2015MigrationStatus +
", publicationDate=" + publicationDate +
", discarded=" + discarded +
", discardedDate=" + discardedDate +
'}';
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/scmspain/exception/NotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.scmspain.exception;

public class NotFoundException extends RuntimeException {

private String message;

public NotFoundException(String message) {
super();
this.message = message;
}

@Override
public String getMessage() {
return message;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/scmspain/repository/ITweetRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.scmspain.repository;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ITweetRepository<T, U> {

void create(T tweet);

T get(U id);

List<T> getAll(Boolean discarded);

void discard(T tweet);

}
53 changes: 53 additions & 0 deletions src/main/java/com/scmspain/repository/TweetRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.scmspain.repository;

import com.scmspain.entities.Tweet;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;

public class TweetRepository implements ITweetRepository<Tweet, Long> {

private EntityManager entityManager;

public TweetRepository(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Override
public void create(Tweet tweet) {
this.entityManager.persist(tweet);
}

@Override
public Tweet get(Long id) {
return this.entityManager.find(Tweet.class, id);
}

@Override
public List<Tweet> getAll(Boolean discarded) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
Root<Tweet> tweet = criteriaQuery.from(Tweet.class);
criteriaQuery.select(tweet);
criteriaQuery.where(
criteriaBuilder.notEqual(tweet.get("pre2015MigrationStatus"), 99),
criteriaBuilder.equal(tweet.get("discarded"), discarded)
);
if (Boolean.FALSE.equals(discarded)) {
criteriaQuery.orderBy(criteriaBuilder.desc(tweet.get("publicationDate")));
} else {
criteriaQuery.orderBy(criteriaBuilder.desc(tweet.get("discardedDate")));
}
Query query = entityManager.createQuery(criteriaQuery);
return query.getResultList();
}

@Override
public void discard(Tweet tweet) {
this.entityManager.merge(tweet);
}
}
Loading