generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from Arquisoft/rest-api
Rest api, updates on page style
- Loading branch information
Showing
36 changed files
with
1,017 additions
and
46 deletions.
There are no files selected for viewing
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
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
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
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
89 changes: 89 additions & 0 deletions
89
src/main/java/com/uniovi/controllers/RestApiController.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,89 @@ | ||
package com.uniovi.controllers; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.uniovi.entities.*; | ||
import com.uniovi.repositories.GameSessionRepository; | ||
import com.uniovi.services.ApiKeyService; | ||
import com.uniovi.services.RestApiService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.lang.reflect.Array; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RestController | ||
public class RestApiController { | ||
private final ApiKeyService apiKeyService; | ||
private final RestApiService restApiService; | ||
|
||
@Autowired | ||
public RestApiController(ApiKeyService apiKeyService, RestApiService restApiService) { | ||
this.apiKeyService = apiKeyService; | ||
this.restApiService = restApiService; | ||
} | ||
|
||
@GetMapping("/api/players") | ||
public String getPlayers(HttpServletResponse response, @RequestParam Map<String, String> params) throws JsonProcessingException { | ||
response.setContentType("application/json"); | ||
ApiKey apiKey = getApiKeyFromParams(params); | ||
if (apiKey == null) { | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
Map<String, String> error = Map.of("error", "Invalid API key"); | ||
return objectMapper.writeValueAsString(error); | ||
} | ||
|
||
List<Player> players = restApiService.getPlayers(params); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
ObjectNode root = objectMapper.createObjectNode(); | ||
ArrayNode arrayNode = objectMapper.createArrayNode(); | ||
for (Player player : players) { | ||
arrayNode.add(player.toJson()); | ||
} | ||
root.put("players", arrayNode); | ||
restApiService.logAccess(apiKey, "/api/players", params); | ||
return root.toString(); | ||
} | ||
|
||
@GetMapping("/api/questions") | ||
public String getQuestions(HttpServletResponse response, @RequestParam Map<String, String> params) throws JsonProcessingException { | ||
response.setContentType("application/json"); | ||
ApiKey apiKey = getApiKeyFromParams(params); | ||
if (apiKey == null) { | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
Map<String, String> error = Map.of("error", "Invalid API key"); | ||
return objectMapper.writeValueAsString(error); | ||
} | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
ObjectNode root = objectMapper.createObjectNode(); | ||
ArrayNode arrayNode = objectMapper.createArrayNode(); | ||
List<Question> questions = restApiService.getQuestions(params); | ||
for (Question question : questions) { | ||
arrayNode.add(question.toJson()); | ||
} | ||
root.set("questions", arrayNode); | ||
restApiService.logAccess(apiKey, "/api/questions", params); | ||
return root.toString(); | ||
} | ||
|
||
private ApiKey getApiKeyFromParams(Map<String, String> params) { | ||
if (!params.containsKey("apiKey")) { | ||
return null; | ||
} | ||
|
||
String apiKey = params.get("apiKey"); | ||
return apiKeyService.getApiKey(apiKey); | ||
} | ||
} |
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
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,29 @@ | ||
package com.uniovi.entities; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class ApiKey { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column(unique = true) | ||
private String keyToken = UUID.randomUUID().toString(); | ||
|
||
@OneToOne | ||
private Player player; | ||
|
||
@OneToMany(mappedBy = "apiKey") | ||
private Set<RestApiAccessLog> accessLogs = new HashSet<>(); | ||
} |
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
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
Oops, something went wrong.