-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
567369d
commit 9d1c50e
Showing
14 changed files
with
2,796 additions
and
1 deletion.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...tp-advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/FootballTeam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
public class FootballTeam { | ||
|
||
private String name; | ||
private String colorTShirt; | ||
private int euroCups; | ||
private Date foundationDate; | ||
private String stadium; | ||
private List<String> keyPlayers; | ||
|
||
public FootballTeam() { | ||
} | ||
|
||
public FootballTeam(String name, String colorTShirt, int euroCups, Date foundationDate, String stadium, | ||
List<String> keyPlayers) { | ||
this.name = name; | ||
this.colorTShirt = colorTShirt; | ||
this.euroCups = euroCups; | ||
this.foundationDate = foundationDate; | ||
this.stadium = stadium; | ||
this.keyPlayers = keyPlayers; | ||
} | ||
|
||
public Date getFoundationDate() { | ||
return foundationDate; | ||
} | ||
|
||
public void setFoundationDate(Date foundationDate) { | ||
this.foundationDate = foundationDate; | ||
} | ||
|
||
public String getStadium() { | ||
return stadium; | ||
} | ||
|
||
public void setStadium(String stadium) { | ||
this.stadium = stadium; | ||
} | ||
|
||
public List<String> getKeyPlayers() { | ||
return keyPlayers; | ||
} | ||
|
||
public void setKeyPlayers(List<String> keyPlayers) { | ||
this.keyPlayers = keyPlayers; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getColorTShirt() { | ||
return colorTShirt; | ||
} | ||
|
||
public void setColorTShirt(String colorTShirt) { | ||
this.colorTShirt = colorTShirt; | ||
} | ||
|
||
public int getEuroCups() { | ||
return euroCups; | ||
} | ||
|
||
public void setEuroCups(int euroCups) { | ||
this.euroCups = euroCups; | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...ced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/FootballTeamResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.DELETE; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
@Path("/football") | ||
public class FootballTeamResource { | ||
|
||
private static final Logger LOG = Logger.getLogger(FootballTeamResource.class); | ||
|
||
private Set<FootballTeam> footballTeamSet = Collections.synchronizedSet(new LinkedHashSet<>()); | ||
|
||
/** | ||
* | ||
* When a JSON extension is installed such as quarkus-rest-jackson or quarkus-rest-jsonb | ||
* Quarkus will use the application/json media type by default for most return values | ||
*/ | ||
|
||
@POST | ||
@Path("/upload-football-json") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Uni<Response> uploadFootballJson(String data) { | ||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
Set<FootballTeam> teamSet = objectMapper.readValue(data, new TypeReference<Set<FootballTeam>>() { | ||
}); | ||
for (FootballTeam footballTeam : teamSet) { | ||
if (footballTeam == null || footballTeam.getName().isEmpty()) { | ||
return Uni.createFrom().item(Response.status(400).entity("Some fields are empty or null").build()); | ||
} else { | ||
footballTeamSet.add(footballTeam); | ||
} | ||
} | ||
return Uni.createFrom() | ||
.item(() -> { | ||
Map<String, Object> responseMap = new HashMap<>(); | ||
responseMap.put("teams", footballTeamSet); | ||
responseMap.put("message", "Teams added successfully"); | ||
return Response.status(201).entity(responseMap).build(); | ||
}); | ||
} catch (JsonMappingException ex) { | ||
LOG.error(ex.getMessage()); | ||
return Uni.createFrom() | ||
.item(Response.status(400).entity("JsonMappingException : " + ex.getMessage()).build()); | ||
} catch (JsonProcessingException e) { | ||
LOG.error(e.getMessage()); | ||
return Uni.createFrom().item( | ||
Response.status(400).entity("Invalid JSON - JsonProcessingException : " + e.getOriginalMessage()).build()); | ||
} | ||
|
||
} | ||
|
||
@DELETE | ||
@Path("/clear") | ||
public Response clearTeams() { | ||
footballTeamSet.clear(); | ||
return Response.ok().build(); | ||
} | ||
|
||
} |
Oops, something went wrong.