🗃️ 👔 Add services, repos, entities, controllers (#2)

While not "production-ready", this branch contains a partially functional back end with an API and a DB.

It can be built upon, as it is.

Co-authored-by: Alexis DRAI <alexis.drai@etu.uca.fr>
Reviewed-on: alexis.drai/AD_MongoDB#2
pull/3/head
Alexis Drai 2 years ago committed by Alexis DRAI
parent 8dae8e3628
commit e66af6f91b

@ -0,0 +1,105 @@
package fr.uca.iut.controllers;
import fr.uca.iut.entities.Pokemong;
import fr.uca.iut.services.PokemongService;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.bson.types.ObjectId;
@Path("/pokemong")
@Produces(MediaType.APPLICATION_JSON)
public class PokemongController {
@Inject
PokemongService pokemongService;
@GET
@Path("/{id}")
public Response getPokemong(@PathParam("id") String id) {
try {
ObjectId objectId = new ObjectId(id);
Pokemong pokemong = pokemongService.getPokemong(objectId);
if (pokemong != null) {
return Response.ok(pokemong)
.build();
}
else {
return Response.status(Response.Status.NOT_FOUND)
.entity("Pokemong not found for id: " + id)
.build();
}
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("Invalid id format: " + id)
.build();
}
}
@GET
public Response getAllPokemongs() {
return Response.ok(pokemongService.getAllPokemongs())
.build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createPokemong(Pokemong pokemong) {
if (pokemongService.isNotMature(pokemong)) {
pokemong.isMegaEvolved = null;
}
Pokemong newPokemong = pokemongService.addPokemong(pokemong);
return Response.status(Response.Status.CREATED)
.entity(newPokemong)
.build();
}
@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updatePokemong(@PathParam("id") String id, Pokemong pokemong) {
try {
if (pokemongService.isNotMature(pokemong)) {
pokemong.isMegaEvolved = null;
}
pokemong.id = new ObjectId(id);
Pokemong updatedPokemong = pokemongService.updatePokemong(pokemong);
if (updatedPokemong != null) {
return Response.status(Response.Status.OK)
.entity(updatedPokemong)
.build();
}
else {
return Response.status(Response.Status.NOT_FOUND)
.entity("Pokemong not found for id: " + id)
.build();
}
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("Invalid id format: " + id)
.build();
}
}
@DELETE
@Path("/{id}")
public Response deletePokemong(@PathParam("id") String id) {
try {
ObjectId objectId = new ObjectId(id);
pokemongService.deletePokemong(objectId);
return Response.ok()
.build();
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("Invalid id format: " + id)
.build();
}
}
}

@ -0,0 +1,13 @@
package fr.uca.iut.entities;
import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;
@MongoEntity(collection = "moves")
public class Move extends PanacheMongoEntity {
public String name;
public String category;
public Integer power;
public Integer accuracy;
public Type type;
}

@ -0,0 +1,25 @@
package fr.uca.iut.entities;
import fr.uca.iut.utils.PokemongName;
import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;
import org.bson.types.ObjectId;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
@MongoEntity(collection = "pokemongs")
public class Pokemong extends PanacheMongoEntity {
public String nickname;
public LocalDate dob;
public Integer level;
public Integer pokedexId;
public Integer evoStage;
public List<PokemongName> evoTrack;
public Boolean isMegaEvolved;
public ObjectId trainer;
public Set<Type> types; // TODO Bound this within [1;2] (in controller)
public List<ObjectId> moveSet; // TODO Bound this within [1;4] (in controller) and denormalize move "name"
}

@ -0,0 +1,18 @@
package fr.uca.iut.entities;
import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;
import org.bson.types.ObjectId;
import java.time.LocalDate;
import java.util.List;
@MongoEntity(collection = "trainers")
public class Trainer extends PanacheMongoEntity {
public String name;
public LocalDate dob;
public Integer wins;
public Integer losses;
public List<ObjectId> pastOpponents;
public List<ObjectId> pokemongs;
}

@ -0,0 +1,30 @@
package fr.uca.iut.entities;
import fr.uca.iut.utils.TypeName;
import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;
import java.util.List;
import java.util.Objects;
@MongoEntity(collection = "types")
public class Type extends PanacheMongoEntity {
public TypeName name;
public List<TypeName> weakAgainst;
public List<TypeName> effectiveAgainst;
@Override
public int hashCode() {
return Objects.hash(name, weakAgainst, effectiveAgainst);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Type type = (Type) o;
return Objects.equals(name, type.name) &&
Objects.equals(weakAgainst, type.weakAgainst) &&
Objects.equals(effectiveAgainst, type.effectiveAgainst);
}
}

@ -0,0 +1,9 @@
package fr.uca.iut.repositories;
import fr.uca.iut.entities.Move;
import io.quarkus.mongodb.panache.PanacheMongoRepository;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class MoveRepository implements PanacheMongoRepository<Move> {
}

@ -0,0 +1,9 @@
package fr.uca.iut.repositories;
import fr.uca.iut.entities.Pokemong;
import io.quarkus.mongodb.panache.PanacheMongoRepository;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class PokemongRepository implements PanacheMongoRepository<Pokemong> {
}

@ -0,0 +1,9 @@
package fr.uca.iut.repositories;
import fr.uca.iut.entities.Trainer;
import io.quarkus.mongodb.panache.PanacheMongoRepository;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class TrainerRepository implements PanacheMongoRepository<Trainer> {
}

@ -0,0 +1,9 @@
package fr.uca.iut.repositories;
import fr.uca.iut.entities.Type;
import io.quarkus.mongodb.panache.PanacheMongoRepository;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class TypeRepository implements PanacheMongoRepository<Type> {
}

@ -0,0 +1,4 @@
package fr.uca.iut.services;
public class MoveService {
}

@ -0,0 +1,64 @@
package fr.uca.iut.services;
import fr.uca.iut.entities.Pokemong;
import fr.uca.iut.repositories.PokemongRepository;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.bson.types.ObjectId;
import java.util.List;
@ApplicationScoped
public class PokemongService {
@Inject
PokemongRepository pokemongRepository;
public Pokemong addPokemong(Pokemong pokemong) {
pokemongRepository.persist(pokemong);
return pokemong;
}
public Pokemong getPokemong(ObjectId id) {
return pokemongRepository.findById(id);
}
public List<Pokemong> getAllPokemongs() {
return pokemongRepository.listAll();
}
public void deletePokemong(ObjectId id) {
Pokemong pokemong = pokemongRepository.findById(id);
if (pokemong != null) {
pokemongRepository.delete(pokemong);
}
}
public Pokemong updatePokemong(Pokemong pokemong) {
Pokemong existingPokemong = pokemongRepository.findById(pokemong.id);
if (existingPokemong != null) {
existingPokemong.nickname = pokemong.nickname;
existingPokemong.dob = pokemong.dob;
existingPokemong.level = pokemong.level;
existingPokemong.pokedexId = pokemong.pokedexId;
existingPokemong.evoStage = pokemong.evoStage;
existingPokemong.evoTrack = pokemong.evoTrack;
existingPokemong.isMegaEvolved = pokemong.isMegaEvolved;
existingPokemong.trainer = pokemong.trainer;
existingPokemong.types = pokemong.types;
existingPokemong.moveSet = pokemong.moveSet;
pokemongRepository.persistOrUpdate(existingPokemong);
}
return existingPokemong;
}
public boolean isNotMature(Pokemong pokemong) {
return pokemong == null
|| pokemong.evoStage == null
|| pokemong.evoTrack == null
|| pokemong.evoTrack.isEmpty()
|| (pokemong.evoStage != pokemong.evoTrack.size() - 1);
}
// TODO PATCH ?
}

@ -0,0 +1,4 @@
package fr.uca.iut.services;
public class TrainerService {
}

@ -0,0 +1,4 @@
package fr.uca.iut.services;
public class TypeService {
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,22 @@
package fr.uca.iut.utils;
public enum TypeName {
NORMAL,
GRASS,
ELECTRIC,
WATER,
FIRE,
BUG,
GHOST,
PSYCHIC,
STEEL,
DARK,
FLYING,
ICE,
POISON,
GROUND,
ROCK,
DRAGON,
FIGHTING,
FAIRY,
}
Loading…
Cancel
Save