service and controller done
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
package fr.eni.demo.bll;
|
||||
|
||||
import fr.eni.demo.bo.Facture;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface FactureService {
|
||||
List<Facture> getByClient(Long clientId);
|
||||
List<Facture> getAll();
|
||||
List<Facture> getUnpayed();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,36 @@
|
||||
package fr.eni.demo.bll;
|
||||
|
||||
import fr.eni.demo.bo.Facture;
|
||||
import fr.eni.demo.dal.FactureRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FactureServiceImpl implements FactureService {
|
||||
|
||||
private final FactureRepository factureRepo;
|
||||
public FactureServiceImpl(FactureRepository factureRepo) {
|
||||
this.factureRepo = factureRepo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Facture> getByClient(Long clientId) {
|
||||
if (clientId == null) {
|
||||
throw new IllegalArgumentException("Client id can't be null");
|
||||
}
|
||||
List<Facture> factures = factureRepo.findByClient(clientId);
|
||||
if (factures == null) {
|
||||
throw new IllegalArgumentException("No factures found");
|
||||
};
|
||||
return factures;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Facture> getAll() {
|
||||
return factureRepo.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Facture> getUnpayed() {
|
||||
return factureRepo.findByDatePayEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,4 +36,8 @@ public class Client {
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "CLIENT_ID")
|
||||
private List<Location> locations;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "CLIENT_ID")
|
||||
private List<Facture> factures;
|
||||
}
|
||||
|
||||
@@ -26,4 +26,8 @@ public class Facture {
|
||||
|
||||
@Column(name = "DATE_PAY")
|
||||
private Date datePay;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "CLIENT_ID", nullable = false)
|
||||
private Client client;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class ClientController {
|
||||
clientService.add(client);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("message", "Client created successfully");
|
||||
response.put("status", true);
|
||||
response.put("status", 201);
|
||||
response.put("data", new HashMap<>());
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
@@ -36,7 +36,7 @@ public class ClientController {
|
||||
List<Client> result = clientService.findByName(name);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("message", "List of Clients found");
|
||||
response.put("status", true);
|
||||
response.put("status", 200);
|
||||
response.put("data", result);
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
|
||||
46
src/main/java/fr/eni/demo/controller/FactureController.java
Normal file
46
src/main/java/fr/eni/demo/controller/FactureController.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package fr.eni.demo.controller;
|
||||
|
||||
import fr.eni.demo.bll.FactureService;
|
||||
import fr.eni.demo.bo.Facture;
|
||||
import fr.eni.demo.bo.Location;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/facture")
|
||||
@RequiredArgsConstructor
|
||||
public class FactureController {
|
||||
|
||||
private final FactureService factureService;
|
||||
|
||||
@GetMapping()
|
||||
public ResponseEntity<Map<String, Object>> getAllFactures() {
|
||||
List<Facture> result = factureService.getAll();
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("data", result);
|
||||
response.put("status", 200);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@GetMapping("/client/{id}")
|
||||
public ResponseEntity<Map<String, Object>> getFactureByClient(@PathVariable Long clientId) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("data", factureService.getByClient(clientId));
|
||||
response.put("status", 200);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@GetMapping("/unpayed")
|
||||
public ResponseEntity<Map<String, Object>> getUnpayedFactures() {
|
||||
List<Facture> result = factureService.getUnpayed();
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("data", result);
|
||||
response.put("status", 200);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,10 @@ package fr.eni.demo.dal;
|
||||
import fr.eni.demo.bo.Facture;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface FactureRepository extends JpaRepository<Facture,Long> {
|
||||
|
||||
List<Facture> findByClient(Long clientId);
|
||||
List<Facture> findByDatePayEmpty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user