Skip to content
Snippets Groups Projects

Issue #18, #19 resolved

5 files
+ 176
0
Compare changes
  • Side-by-side
  • Inline
Files
5
package nl.utwente.mod4.pokemon.dao;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.NotFoundException;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import nl.utwente.mod4.pokemon.Utils;
import nl.utwente.mod4.pokemon.model.Pokemon;
import nl.utwente.mod4.pokemon.model.Trainer;
public enum PokemonDao {
INSTANCE;
private static final String ORIGINAL_POKEMON = Utils.getAbsolutePathToResources() + "/default-pokemon-dataset.json";
private static final String POKEMON = Utils.getAbsolutePathToResources() + "/pokemon.json";
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
private HashMap<String, Pokemon> pokemons = new HashMap<>();
public void delete(String id) {
if(pokemons.containsKey(id)) {
pokemons.remove(id);
} else {
throw new NotFoundException("Pokemon '" + id + "' not found.");
}
}
public List<Pokemon> getPokemon(int pageSize, int pageNumber) {
var list = new ArrayList<>(pokemons.values());
return (List<Pokemon>) Utils.pageSlice(list,pageSize,pageNumber);
}
public Pokemon getTrainer(String id) {
var pt = pokemons.get(id);
if (pt == null) {
throw new NotFoundException("Pokemon '" + id + "' not found!");
}
return pt;
}
public void load() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File source = existsPokemon() ?
new File(POKEMON) :
new File(ORIGINAL_POKEMON);
Pokemon[] arr = mapper.readValue(source, Pokemon[].class);
Arrays.stream(arr).forEach(pokemon -> pokemons.put(pokemon.id, pokemon));
}
public void save() throws IOException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
File destination = new File(POKEMON);
writer.writeValue(destination, pokemons.values());
}
private boolean existsPokemon() {
File f = new File(POKEMON);
return f.exists() && !f.isDirectory();
}
private int getMaxId() {
Set<String> ids = pokemons.keySet();
return ids.isEmpty() ? 0 : ids.stream()
.map(Integer::parseInt)
.max(Integer::compareTo)
.get();
}
public Pokemon create(Pokemon newPokemon) {
String nextId = "" + (getMaxId() + 1);
newPokemon.id = nextId;
Instant now = Instant.now();
newPokemon.created = dateFormatter.format(now.atZone(ZoneId.systemDefault()).toLocalDateTime());
newPokemon.lastUpDate = dateFormatter.format(now.atZone(ZoneId.systemDefault()).toLocalDateTime());
pokemons.put(nextId,newPokemon);
return newPokemon;
}
public Pokemon replace(Pokemon updated) {
if(!updated.checkIsValid())
throw new BadRequestException("Invalid pokemon.");
if(pokemons.get(updated.id) == null)
throw new NotFoundException("Pokemon id '" + updated.id + "' not found.");
Instant now = Instant.now();
updated.created = pokemons.get(updated.id).created;
updated.lastUpDate = dateFormatter.format(now.atZone(ZoneId.systemDefault()).toLocalDateTime());
pokemons.put(updated.id,updated);
return updated;
}
public int getTotalPokemon() {
return pokemons.keySet().size();
}
}
Loading