added possibility to buy facilities and ships
This commit is contained in:
2
mvnw
vendored
2
mvnw
vendored
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/sh
|
()#!/bin/sh
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
# Licensed to the Apache Software Foundation (ASF) under one
|
# Licensed to the Apache Software Foundation (ASF) under one
|
||||||
# or more contributor license agreements. See the NOTICE file
|
# or more contributor license agreements. See the NOTICE file
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.context.annotation.Profile;
|
import org.springframework.context.annotation.Profile;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@@ -15,7 +16,8 @@ public class SpringSecurityConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
|
|
||||||
http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
|
http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll())
|
||||||
|
.csrf(AbstractHttpConfigurer::disable);
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package bzh.risotto.galactik.api.controler;
|
package bzh.risotto.galactik.api.controller;
|
||||||
|
|
||||||
import bzh.risotto.galactik.api.model.Corpo;
|
import bzh.risotto.galactik.api.model.Corpo;
|
||||||
import bzh.risotto.galactik.api.service.CorpoService;
|
import bzh.risotto.galactik.api.service.CorpoService;
|
||||||
@@ -8,8 +8,6 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/corpo")
|
@RequestMapping("/corpo")
|
||||||
public class CorpoController {
|
public class CorpoController {
|
||||||
@@ -27,7 +25,7 @@ public class CorpoController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{corpoId}")
|
@GetMapping("/{corpoId}")
|
||||||
public Optional<Corpo> getCorpos(@PathVariable Long corpoId) {
|
public Corpo getCorpos(@PathVariable Long corpoId) {
|
||||||
return this.corpoService.getCorpo(corpoId);
|
return this.corpoService.getCorpo(corpoId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package bzh.risotto.galactik.api.controler;
|
package bzh.risotto.galactik.api.controller;
|
||||||
|
|
||||||
import bzh.risotto.galactik.api.model.Facility;
|
import bzh.risotto.galactik.api.model.Facility;
|
||||||
import bzh.risotto.galactik.api.service.FacilityService;
|
import bzh.risotto.galactik.api.service.FacilityService;
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package bzh.risotto.galactik.api.controller;
|
||||||
|
|
||||||
|
import bzh.risotto.galactik.api.model.Facility;
|
||||||
|
import bzh.risotto.galactik.api.model.Ship;
|
||||||
|
import bzh.risotto.galactik.api.service.MarketService;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/market")
|
||||||
|
public class MarketController {
|
||||||
|
|
||||||
|
private final MarketService marketService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public MarketController(MarketService marketService) {
|
||||||
|
this.marketService = marketService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/facilities")
|
||||||
|
public List<Facility> getFacilities() {
|
||||||
|
|
||||||
|
return marketService.getFacilities();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = "/facilities/buy", consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public Facility buyFacilities(@RequestBody JsonNode data) {
|
||||||
|
|
||||||
|
long facilityId = Long.parseLong(data.get("facilityId").asText());
|
||||||
|
long corpoId = Long.parseLong(data.get("corpoId").asText());
|
||||||
|
|
||||||
|
return marketService.buyFacilities(facilityId, corpoId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/ships")
|
||||||
|
public List<Ship> getShips() {
|
||||||
|
|
||||||
|
return marketService.getShips();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = "/ships/buy", consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public Ship buyShip(@RequestBody JsonNode data) {
|
||||||
|
|
||||||
|
long facilityId = Long.parseLong(data.get("shipId").asText());
|
||||||
|
long corpoId = Long.parseLong(data.get("corpoId").asText());
|
||||||
|
|
||||||
|
return marketService.buyShip(facilityId, corpoId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package bzh.risotto.galactik.api.controler;
|
package bzh.risotto.galactik.api.controller;
|
||||||
|
|
||||||
import bzh.risotto.galactik.api.service.ModuleService;
|
import bzh.risotto.galactik.api.service.ModuleService;
|
||||||
import bzh.risotto.galactik.api.model.Module;
|
import bzh.risotto.galactik.api.model.Module;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package bzh.risotto.galactik.api.controler;
|
package bzh.risotto.galactik.api.controller;
|
||||||
|
|
||||||
import bzh.risotto.galactik.api.model.Planet;
|
import bzh.risotto.galactik.api.model.Planet;
|
||||||
import bzh.risotto.galactik.api.service.PlanetService;
|
import bzh.risotto.galactik.api.service.PlanetService;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package bzh.risotto.galactik.api.controler;
|
package bzh.risotto.galactik.api.controller;
|
||||||
|
|
||||||
import bzh.risotto.galactik.api.model.Ressource;
|
import bzh.risotto.galactik.api.model.Ressource;
|
||||||
import bzh.risotto.galactik.api.service.RessourceService;
|
import bzh.risotto.galactik.api.service.RessourceService;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package bzh.risotto.galactik.api.controler;
|
package bzh.risotto.galactik.api.controller;
|
||||||
|
|
||||||
import bzh.risotto.galactik.api.model.Ship;
|
import bzh.risotto.galactik.api.model.Ship;
|
||||||
import bzh.risotto.galactik.api.service.ShipService;
|
import bzh.risotto.galactik.api.service.ShipService;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package bzh.risotto.galactik.api.controler;
|
package bzh.risotto.galactik.api.controller;
|
||||||
|
|
||||||
import bzh.risotto.galactik.api.model.Star;
|
import bzh.risotto.galactik.api.model.Star;
|
||||||
import bzh.risotto.galactik.api.service.StarService;
|
import bzh.risotto.galactik.api.service.StarService;
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package bzh.risotto.galactik.api.exceptions;
|
||||||
|
|
||||||
|
public class CorpoNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public CorpoNotFoundException(Long id) {
|
||||||
|
super("Corpo: " + id + " not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
public CorpoNotFoundException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package bzh.risotto.galactik.api.exceptions;
|
||||||
|
|
||||||
|
public class FacilityNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public FacilityNotFoundException(Long id) {
|
||||||
|
super("Facilty: " + id + " not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
public FacilityNotFoundException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package bzh.risotto.galactik.api.exceptions;
|
||||||
|
|
||||||
|
public class NotEnoughtRessourceException extends RuntimeException {
|
||||||
|
|
||||||
|
public NotEnoughtRessourceException(long ressourceId, long corpoId) {
|
||||||
|
super("corpo: " + corpoId + " has not enough resource of type: " + ressourceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotEnoughtRessourceException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package bzh.risotto.galactik.api.exceptions;
|
||||||
|
|
||||||
|
public class RessourceNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public RessourceNotFoundException(int ressourceId) {
|
||||||
|
super("Ressource: " + ressourceId + " not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
public RessourceNotFoundException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package bzh.risotto.galactik.api.exceptions;
|
||||||
|
|
||||||
|
public class ShipNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public ShipNotFoundException(long shipId) {
|
||||||
|
super("Ship: " + shipId + " not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
public ShipNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@ package bzh.risotto.galactik.api.model;
|
|||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.hibernate.annotations.Fetch;
|
||||||
|
import org.hibernate.annotations.FetchMode;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -13,14 +15,11 @@ public class Corpo {
|
|||||||
@Id
|
@Id
|
||||||
@Column(name = "c_id")
|
@Column(name = "c_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "c_name")
|
@Column(name = "c_name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column(name = "c_money")
|
|
||||||
private Double money;
|
|
||||||
|
|
||||||
@OneToMany()
|
@OneToMany()
|
||||||
@JoinTable(
|
@JoinTable(
|
||||||
name = "corpo_ressource",
|
name = "corpo_ressource",
|
||||||
@@ -29,7 +28,8 @@ public class Corpo {
|
|||||||
)
|
)
|
||||||
private List<Ressource> ressources;
|
private List<Ressource> ressources;
|
||||||
|
|
||||||
@OneToMany(fetch = FetchType.LAZY)
|
@OneToMany()
|
||||||
|
@Fetch(FetchMode.JOIN)
|
||||||
@JoinColumn(name = "s_propritary")
|
@JoinColumn(name = "s_propritary")
|
||||||
private List<Ship> ships;
|
private List<Ship> ships;
|
||||||
|
|
||||||
@@ -38,6 +38,7 @@ public class Corpo {
|
|||||||
private List<Planet> planets;
|
private List<Planet> planets;
|
||||||
|
|
||||||
@OneToMany()
|
@OneToMany()
|
||||||
|
@Fetch(FetchMode.JOIN)
|
||||||
@JoinColumn(name = "f_proprietary", referencedColumnName = "c_id")
|
@JoinColumn(name = "f_proprietary", referencedColumnName = "c_id")
|
||||||
private List<Facility> facilities;
|
private List<Facility> facilities;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ import java.util.Date;
|
|||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "facility")
|
@Table(name = "facility")
|
||||||
public class Facility {
|
public class Facility implements Item {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "f_id")
|
@Column(name = "f_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private long id;
|
||||||
|
|
||||||
@OneToOne()
|
@OneToOne()
|
||||||
@JoinColumn(name = "f_type", referencedColumnName = "ft_id")
|
@JoinColumn(name = "f_type", referencedColumnName = "ft_id")
|
||||||
@@ -30,5 +30,19 @@ public class Facility {
|
|||||||
private Integer planet;
|
private Integer planet;
|
||||||
|
|
||||||
@Column(name = "f_proprietary")
|
@Column(name = "f_proprietary")
|
||||||
private Integer corpo;
|
private long corpo;
|
||||||
|
|
||||||
|
|
||||||
|
public Facility copy() {
|
||||||
|
Facility f = new Facility();
|
||||||
|
|
||||||
|
//f.setId(this.id);
|
||||||
|
f.setFacilityType(this.facilityType);
|
||||||
|
f.setProcessingRessource(this.processingRessource);
|
||||||
|
f.setStartTime(this.startTime);
|
||||||
|
f.setPlanet(this.planet);
|
||||||
|
f.setCorpo(this.corpo);
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,14 @@ public class FacilityType {
|
|||||||
@Id
|
@Id
|
||||||
@Column(name = "ft_id")
|
@Column(name = "ft_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Integer id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "ft_name")
|
@Column(name = "ft_name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@Column(name = "ft_value")
|
||||||
|
private int value;
|
||||||
|
|
||||||
@OneToOne()
|
@OneToOne()
|
||||||
@JoinColumn(name = "ft_ressource_in", referencedColumnName = "rt_id")
|
@JoinColumn(name = "ft_ressource_in", referencedColumnName = "rt_id")
|
||||||
private RessourceType ressourceIn;
|
private RessourceType ressourceIn;
|
||||||
|
|||||||
9
src/main/java/bzh/risotto/galactik/api/model/Item.java
Normal file
9
src/main/java/bzh/risotto/galactik/api/model/Item.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package bzh.risotto.galactik.api.model;
|
||||||
|
|
||||||
|
public interface Item {
|
||||||
|
|
||||||
|
public long getId();
|
||||||
|
|
||||||
|
public Item copy();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@ public class Module {
|
|||||||
@Id
|
@Id
|
||||||
@Column(name = "m_id")
|
@Column(name = "m_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "m_name")
|
@Column(name = "m_name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class Planet {
|
|||||||
@Id
|
@Id
|
||||||
@Column(name = "p_id")
|
@Column(name = "p_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Integer id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "p_name")
|
@Column(name = "p_name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ public class Ressource {
|
|||||||
@Id
|
@Id
|
||||||
@Column(name = "r_id")
|
@Column(name = "r_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private long id;
|
||||||
|
|
||||||
@OneToOne(cascade = CascadeType.ALL)
|
@OneToOne(cascade = CascadeType.ALL)
|
||||||
@JoinColumn(name = "r_type", referencedColumnName = "rt_id")
|
@JoinColumn(name = "r_type", referencedColumnName = "rt_id")
|
||||||
private RessourceType type;
|
private RessourceType type;
|
||||||
|
|
||||||
@Column(name = "r_quantity")
|
@Column(name = "r_quantity")
|
||||||
private Integer quantity;
|
private int quantity;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ public class RessourceType {
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "rt_id")
|
@Column(name = "rt_id")
|
||||||
private Long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "rt_name")
|
@Column(name = "rt_name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|||||||
@@ -2,24 +2,29 @@ package bzh.risotto.galactik.api.model;
|
|||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.hibernate.annotations.Fetch;
|
||||||
|
import org.hibernate.annotations.FetchMode;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
@Table(name = "ship")
|
@Table(name = "ship")
|
||||||
public class Ship {
|
public class Ship implements Item {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "s_id")
|
@Column(name = "s_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "s_name")
|
@Column(name = "s_name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@Column(name = "s_propritary")
|
||||||
|
private long proprietary;
|
||||||
|
|
||||||
@Column(name = "s_value")
|
@Column(name = "s_value")
|
||||||
private float value;
|
private int value;
|
||||||
|
|
||||||
@Column(name = "s_size")
|
@Column(name = "s_size")
|
||||||
private int size;
|
private int size;
|
||||||
@@ -31,6 +36,7 @@ public class Ship {
|
|||||||
private float speed;
|
private float speed;
|
||||||
|
|
||||||
@OneToMany()
|
@OneToMany()
|
||||||
|
@Fetch(FetchMode.JOIN)
|
||||||
@JoinTable(
|
@JoinTable(
|
||||||
name = "ship_modules",
|
name = "ship_modules",
|
||||||
joinColumns = @JoinColumn(name = "sm_ship_id"),
|
joinColumns = @JoinColumn(name = "sm_ship_id"),
|
||||||
@@ -39,5 +45,20 @@ public class Ship {
|
|||||||
List<Module> modules;
|
List<Module> modules;
|
||||||
|
|
||||||
@Column(name = "s_planet")
|
@Column(name = "s_planet")
|
||||||
private int planet;
|
private Integer planet;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item copy() {
|
||||||
|
Ship ship = new Ship();
|
||||||
|
ship.setName(this.name);
|
||||||
|
ship.setProprietary(this.proprietary);
|
||||||
|
ship.setModules(this.modules);
|
||||||
|
ship.setPlanet(this.planet);
|
||||||
|
ship.setValue(this.value);
|
||||||
|
ship.setSize(this.size);
|
||||||
|
ship.setPower(this.power);
|
||||||
|
ship.setSpeed(this.speed);
|
||||||
|
|
||||||
|
return ship;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,16 +13,16 @@ public class Star {
|
|||||||
@Id
|
@Id
|
||||||
@Column(name = "st_id")
|
@Column(name = "st_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "st_name")
|
@Column(name = "st_name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column(name = "pos_x")
|
@Column(name = "pos_x")
|
||||||
private Double posX;
|
private double posX;
|
||||||
|
|
||||||
@Column(name = "pos_y")
|
@Column(name = "pos_y")
|
||||||
private Double posY;
|
private double posY;
|
||||||
|
|
||||||
@OneToMany()
|
@OneToMany()
|
||||||
@JoinColumn(name = "p_star")
|
@JoinColumn(name = "p_star")
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
package bzh.risotto.galactik.api.service;
|
package bzh.risotto.galactik.api.service;
|
||||||
|
|
||||||
import bzh.risotto.galactik.api.model.Corpo;
|
import bzh.risotto.galactik.api.exceptions.CorpoNotFoundException;
|
||||||
|
import bzh.risotto.galactik.api.exceptions.NotEnoughtRessourceException;
|
||||||
|
import bzh.risotto.galactik.api.exceptions.RessourceNotFoundException;
|
||||||
|
import bzh.risotto.galactik.api.model.*;
|
||||||
import bzh.risotto.galactik.api.repository.CorpoRepository;
|
import bzh.risotto.galactik.api.repository.CorpoRepository;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -14,16 +18,86 @@ public class CorpoService {
|
|||||||
|
|
||||||
private final CorpoRepository corpoRepository;
|
private final CorpoRepository corpoRepository;
|
||||||
|
|
||||||
|
private FacilityService facilityService;
|
||||||
|
private ShipService shipService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public CorpoService(CorpoRepository corpoRepository) {
|
public CorpoService(CorpoRepository corpoRepository, FacilityService facilityService, ShipService shipService) {
|
||||||
this.corpoRepository = corpoRepository;
|
this.corpoRepository = corpoRepository;
|
||||||
|
|
||||||
|
this.facilityService = facilityService;
|
||||||
|
this.shipService = shipService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterable<Corpo> getCorpos() {
|
public Iterable<Corpo> getCorpos() {
|
||||||
return this.corpoRepository.findAll();
|
return this.corpoRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Corpo> getCorpo(Long id) {
|
public Corpo getCorpo(long id) {
|
||||||
return this.corpoRepository.findById(id);
|
Optional<Corpo> res = this.corpoRepository.findById(id);
|
||||||
|
|
||||||
|
if (res.isEmpty()) {
|
||||||
|
throw new CorpoNotFoundException(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addFacilities(Facility facility, long corpoId) {
|
||||||
|
|
||||||
|
Corpo corpo = this.getCorpo(corpoId);
|
||||||
|
|
||||||
|
facility.setCorpo(corpoId);
|
||||||
|
List<Facility> facilities = corpo.getFacilities();
|
||||||
|
|
||||||
|
facilities.add(facility);
|
||||||
|
corpo.setFacilities(facilities);
|
||||||
|
|
||||||
|
this.facilityService.save(facility);
|
||||||
|
this.corpoRepository.save(corpo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addShip(Ship ship, long corpoId) {
|
||||||
|
|
||||||
|
Corpo corpo = this.getCorpo(corpoId);
|
||||||
|
|
||||||
|
ship.setProprietary(corpoId);
|
||||||
|
List<Ship> ships = corpo.getShips();
|
||||||
|
|
||||||
|
ships.add(ship);
|
||||||
|
corpo.setShips(ships);
|
||||||
|
|
||||||
|
this.shipService.save(ship);
|
||||||
|
this.corpoRepository.save(corpo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean updateRessource(long ressourceId, int value, long corpoId) {
|
||||||
|
|
||||||
|
boolean res = false;
|
||||||
|
|
||||||
|
Corpo corpo = this.getCorpo(corpoId);
|
||||||
|
|
||||||
|
Ressource ressource = null;
|
||||||
|
for (Ressource r : corpo.getRessources()) {
|
||||||
|
if (ressourceId == r.getId()) {
|
||||||
|
ressource = r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ressource == null) {
|
||||||
|
throw new RessourceNotFoundException("ressource :" + ressourceId + "not found for corpo: " + corpoId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ressource.getQuantity() <= value) {
|
||||||
|
throw new NotEnoughtRessourceException(ressourceId, corpoId);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res = true;
|
||||||
|
|
||||||
|
// edit qt by reference
|
||||||
|
ressource.setQuantity(ressource.getQuantity() + value);
|
||||||
|
}
|
||||||
|
corpoRepository.save(corpo);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,13 @@ public class FacilityService {
|
|||||||
return this.facilityRepository.findAll();
|
return this.facilityRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Facility> getFacility(Long id) {
|
public Optional<Facility> getFacility(long id) {
|
||||||
return this.facilityRepository.findById(id);
|
return this.facilityRepository.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void save(Facility facility) {
|
||||||
|
|
||||||
|
this.facilityRepository.save(facility);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package bzh.risotto.galactik.api.service;
|
||||||
|
|
||||||
|
import bzh.risotto.galactik.api.exceptions.FacilityNotFoundException;
|
||||||
|
import bzh.risotto.galactik.api.exceptions.ShipNotFoundException;
|
||||||
|
import bzh.risotto.galactik.api.model.Corpo;
|
||||||
|
import bzh.risotto.galactik.api.model.Facility;
|
||||||
|
import bzh.risotto.galactik.api.model.Item;
|
||||||
|
import bzh.risotto.galactik.api.model.Ship;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.crossstore.ChangeSetPersister;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.swing.text.html.parser.Entity;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Service
|
||||||
|
public class MarketService {
|
||||||
|
|
||||||
|
private CorpoService corpoService;
|
||||||
|
|
||||||
|
private Long marketCorpoId;
|
||||||
|
private Corpo marketCorpo;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public MarketService(CorpoService corpoService) {
|
||||||
|
this.corpoService = corpoService;
|
||||||
|
|
||||||
|
this.marketCorpoId = 1L;
|
||||||
|
this.marketCorpo = corpoService.getCorpo(marketCorpoId);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Facility> getFacilities() {
|
||||||
|
return this.marketCorpo.getFacilities();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Facility buyFacilities(long facilityId, long corpoId) {
|
||||||
|
|
||||||
|
List<Facility> facilities = this.marketCorpo.getFacilities();
|
||||||
|
Facility facility = findItem(facilities, facilityId);
|
||||||
|
|
||||||
|
if (facility == null) {
|
||||||
|
throw new FacilityNotFoundException("Facility not found (" + facilityId + ") in corpo (" + this.marketCorpoId + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean status = corpoService.updateRessource(4, -facility.getFacilityType().getValue(), corpoId);
|
||||||
|
if (status) {
|
||||||
|
corpoService.addFacilities(facility, corpoId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return facility;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Ship> getShips() {
|
||||||
|
return this.marketCorpo.getShips();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Ship buyShip(long shipId, long corpoId) {
|
||||||
|
List<Ship> ships = this.marketCorpo.getShips();
|
||||||
|
Ship ship = this.findItem(ships, shipId);
|
||||||
|
|
||||||
|
if (ship == null) {
|
||||||
|
throw new ShipNotFoundException(shipId);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean status = corpoService.updateRessource(4, -ship.getValue(), corpoId);
|
||||||
|
if (status) {
|
||||||
|
corpoService.addShip(ship, corpoId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ship;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <C extends Item> C findItem(List<C> items, long itemId) {
|
||||||
|
C item = null;
|
||||||
|
for (C i : items) {
|
||||||
|
if (itemId == i.getId()) {
|
||||||
|
|
||||||
|
item = (C) i.copy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ public class ModuleService {
|
|||||||
return this.moduleRepository.findAll();
|
return this.moduleRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Module> getModule(Long id) {
|
public Optional<Module> getModule(long id) {
|
||||||
return this.moduleRepository.findById(id);
|
return this.moduleRepository.findById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class PlanetService {
|
|||||||
return this.planetRepository.findAll();
|
return this.planetRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Planet> getPlanet(Long id) {
|
public Optional<Planet> getPlanet(long id) {
|
||||||
return this.planetRepository.findById(id);
|
return this.planetRepository.findById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class RessourceService {
|
|||||||
return this.ressourceRepository.findAll();
|
return this.ressourceRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Ressource> getRessource(Long id) {
|
public Optional<Ressource> getRessource(long id) {
|
||||||
return this.ressourceRepository.findById(id);
|
return this.ressourceRepository.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,11 @@ public class ShipService {
|
|||||||
return this.shipRepository.findAll();
|
return this.shipRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Ship> getShip(Long id) {
|
public Optional<Ship> getShip(long id) {
|
||||||
return this.shipRepository.findById(id);
|
return this.shipRepository.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void save(Ship ship) {
|
||||||
|
this.shipRepository.save(ship);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class StarService {
|
|||||||
return starRepository.findAll();
|
return starRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Star> getStar(Long id) {
|
public Optional<Star> getStar(long id) {
|
||||||
return starRepository.findById(id);
|
return starRepository.findById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,14 @@ server.port=8081
|
|||||||
# spring security
|
# spring security
|
||||||
spring.profiles.active=dev
|
spring.profiles.active=dev
|
||||||
|
|
||||||
|
spring.mvc.log-request-details=true
|
||||||
|
|
||||||
|
# log
|
||||||
|
logging.level.root=debug
|
||||||
|
logging.level.risotto=debug
|
||||||
|
logging.level.org.springframework.boot.autoconfigure.tomcat=debug
|
||||||
|
logging.level.org.springframework.boot.autoconfigure.sql=debug
|
||||||
|
|
||||||
# sql
|
# sql
|
||||||
spring.datasource.url=jdbc:mysql://localhost:3306/galactifeur
|
spring.datasource.url=jdbc:mysql://localhost:3306/galactifeur
|
||||||
spring.datasource.username=gltk_user
|
spring.datasource.username=gltk_user
|
||||||
|
|||||||
Reference in New Issue
Block a user