Having a MVC pattern Sprinboot application we want the user to send a POST call to register a new PuntoDiInteresse document into a MongoDB collection. The verificato field is entitled to show if the point has been validated by a Curatore so when we create a new one that field must be on false until the Curatore proceed to validate it.
Model:
package it.unicam.cs.LocalDevelopmentPlatform.luoghi;
import jakarta.persistence.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Objects;
@Document(collection = "PuntoDiInteresse")
public class PuntoDiInteresse {
@Id
private int _id;
private Coordinata coordinata;
private String nome;
private TipologiaPunto tipologia;
private boolean verificato;
private boolean segnalato;
private String descrizione;
private String motivoSegnalazione;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PuntoDiInteresse that)) return false;
return verificato == that.verificato && segnalato == that.segnalato && Objects.equals(coordinata, that.coordinata)
&& Objects.equals(nome, that.nome) && tipologia == that.tipologia && Objects.equals(descrizione, that.descrizione)
&& Objects.equals(motivoSegnalazione, that.motivoSegnalazione);
}
public PuntoDiInteresse(Coordinata coordinata, String nome, TipologiaPunto tipologia, String descrizione) {
//TODO bisogna vedere come mai il deserializzatore bypassi totalmente questo costruttore quando va a creare un'istanza
// e quindi si può fare injection e forzare il campo a true del verificato e del segnalato.
this.coordinata = coordinata;
this.nome = nome;
this.tipologia = tipologia;
this.descrizione = descrizione;
this.setMotivoSegnalazione(null);
this.setVerificato(false);
this.setSegnalato(false);
this._id = this.hashCode();
}
@Override
public int hashCode() {
return Math.abs(Objects.hash(coordinata, nome, tipologia, descrizione));
}
public int get_id() {
return _id;
}
public void setSegnalato(boolean segnalato) {
this.segnalato = segnalato;
}
public void setVerificato(boolean verificato) {
this.verificato = verificato;
}
public Coordinata getCoordinata() {
return coordinata;
}
public String getNome() {
return nome;
}
public TipologiaPunto getTipologia() {
return tipologia;
}
public boolean isVerificato() {return verificato; }
public void verifica(){ this.verificato = true; }
public boolean isSegnalato() {return segnalato; }
public void segnala(){ this.segnalato= true; }
public String getDescrizione() {
return descrizione;
}
public String getMotivoSegnalazione() {
return motivoSegnalazione;
}
public void setMotivoSegnalazione(String motivoSegnalazione) {
this.motivoSegnalazione = motivoSegnalazione;
}
}
Repository:
package it.unicam.cs.LocalDevelopmentPlatform.repository;
import it.unicam.cs.LocalDevelopmentPlatform.luoghi.PuntoDiInteresse;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PuntoDiInteresseRepo extends MongoRepository<PuntoDiInteresse, java.lang.Integer> {
@Query("{ '_id' : ?0 }")
PuntoDiInteresse findById(int id);
@Query("{verificato : true}")
@Override
List<PuntoDiInteresse> findAll();
@Query("{verificato : false}")
List<PuntoDiInteresse> findAllFalse();
}
Controller:
package it.unicam.cs.LocalDevelopmentPlatform.controller;
import it.unicam.cs.LocalDevelopmentPlatform.luoghi.Itinerario;
import it.unicam.cs.LocalDevelopmentPlatform.service.PuntoInteresseService;
import org.springframework.web.bind.annotation.*;
import it.unicam.cs.LocalDevelopmentPlatform.luoghi.PuntoDiInteresse;
import it.unicam.cs.LocalDevelopmentPlatform.service.ItinerarioService;
@RestController
@RequestMapping("contributor")
public class ContributorController extends TuristaController {
public ContributorController(PuntoInteresseService puntoInteresseService, ItinerarioService itinerarioService) {
super(puntoInteresseService, itinerarioService);
}
@PostMapping(value = "/caricaPunto")
public void caricaPunto(@RequestBody PuntoDiInteresse puntoDiInteresse) {
puntoInteresseService.savePunto(puntoDiInteresse);
}
@PostMapping("/caricaItinerario")
public void caricaItinerario(@RequestBody Itinerario itinerario){itinerarioService.saveItinerario(itinerario);}
}
Service:
package it.unicam.cs.LocalDevelopmentPlatform.service;
import it.unicam.cs.LocalDevelopmentPlatform.luoghi.PuntoDiInteresse;
import it.unicam.cs.LocalDevelopmentPlatform.repository.PuntoDiInteresseRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PuntoInteresseService {
private final PuntoDiInteresseRepo puntoDiInteresseRepo;
@Autowired
public PuntoInteresseService(PuntoDiInteresseRepo puntoDiInteresseRepo) {
this.puntoDiInteresseRepo = puntoDiInteresseRepo;
}
public List<PuntoDiInteresse> getAllPunti() {
return puntoDiInteresseRepo.findAll();
}
public List<PuntoDiInteresse> getAllPuntiById(List<Integer> punti){return puntoDiInteresseRepo.findAllById(punti);}
public PuntoDiInteresse getPuntoByID(int id) {
return puntoDiInteresseRepo.findById(id);
}
public void savePunto(PuntoDiInteresse puntoDiInteresse) {
puntoDiInteresseRepo.save(puntoDiInteresse);
}
public void deletePunto(int id){puntoDiInteresseRepo.deleteById(id);}
public List<PuntoDiInteresse> getAllNotVerified() {
return puntoDiInteresseRepo.findAllFalse();
}
public void verificaPunto(int id) {
PuntoDiInteresse daVer = puntoDiInteresseRepo.findById(id);
daVer.verifica();
puntoDiInteresseRepo.deleteById(id);
puntoDiInteresseRepo.save(daVer);
}
}
POSTMAN request:
{
"coordinata": {
"latitudine": "{{randomLat}}",
"longitudine": "{{randomLong}}"
},
"nome": "Name",
"tipologia": "{{randomTipologia}}",
"verificato": "true",
"descrizione": "Description"
}
We tried to put some constraint on the constructor using default parameter, using the set function and even rewrite a JSON deserializer but the program continues to let the user to create a new document forcing the field to true.
MadLuke312 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.