The fest has two technical rounds based on the results of each round the participants should be segregated in the database. If the participants have cleared the first round with result ‘> 70% ’ then the developer of this project need to create marks along with participants details (i.e., Name, Result) in new table that shows the details along with second round.
For this exercise, you need to create a separate table for eligible participants and add all the participants whose test result is greater than 70% in the table.
In our spring boot application we have created in the prvious exercise.We need to add a method to update the participants score and also an endpoint which returns list of eligible participants and also adds them in the new table.
Create a eligible participants pojo class with all the same attributes as the participant details
Skill9EligibleParticipants.java
package in.anreddy.techfest;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Skill9EligibleParticipants {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String email;
private int test1score;
private int test2score;
public String getName() {
return name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public int getTest2score() {
return test2score;
}
public void setTest2score(int test2score) {
this.test2score = test2score;
}
public int getTest1score() {
return test1score;
}
public void setTest1score(int test1score) {
this.test1score = test1score;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setName(String name) {
this.name = name;
}
}
now let us create a eligible repository class
package in.anreddy.techfest;
import org.springframework.data.repository.CrudRepository;
public interface EligibleRepository extends
CrudRepository<Skill9EligibleParticipants, Integer> {
}
and also in the participant repository class add two methods findByTest1scoreGreaterThan, findByEmail.
The findByEmail method will take a string email as input and returns a participant object if a participant with that email is found or else returns null.
The findByTest1scoreGreaterThan method will take an integer score as input and returns a list of participants whose test1score is greater than the input score.
ParticipantRepository.java
package in.anreddy.techfest;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ParticipantRepository extends CrudRepository<Skill9ParticipantDetails, Integer> {
List<Skill9ParticipantDetails> findByTest1scoreGreaterThan(int test1score);
Skill9ParticipantDetails findByEmail(String email);
}
In our MainController class we need to add a method to update the score of the participant this method will be a put method and will take email and mark as parameters then it will check if the participant is present in the database and if present then it will update the score of the participant.
Now we will add a get method to get the list of eligible participants and also to add them to the eligible participants table.
This method will get the list of eligible participants from the participantRepository then it will delete all the paticipants in eligible participants table and then add all the new eligible participants to the eligible participants table.
Our Final MainController class will look like this
package in.anreddy.techfest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MainController {
@Autowired
private ParticipantRepository participantRepository;
@Autowired
private OrganizerRepository organizerRepository;
@Autowired
private EligibleRepository eligibleRepository;
@PostMapping(path = "/add-participant") // Map ONLY POST Requests
public @ResponseBody String addNewParticipant(@RequestParam String name, @RequestParam String email) {
Skill9ParticipantDetails n = new Skill9ParticipantDetails();
n.setName(name);
n.setEmail(email);
participantRepository.save(n);
return "Participant added successfully";
}
@PutMapping(path = "/add-participant-marks")
public @ResponseBody String updateParticipantMarks(@RequestParam String email, @RequestParam int mark) {
Skill9ParticipantDetails p = participantRepository.findByEmail(email);
if (p == null) {
return "Participant not found";
} else {
p.setTest1score(mark);
participantRepository.save(p);
return "Participant updated successfully";
}
}
@GetMapping(path = "/all-participants")
public @ResponseBody Iterable<Skill9ParticipantDetails> getAllParticipants() {
// This returns a JSON or XML with the users
return participantRepository.findAll();
}
@GetMapping(path = "/all-organizers")
public @ResponseBody Iterable<Skill9FestOrganizers> getAllOrganizers() {
// This returns a JSON or XML with the users
return organizerRepository.findAll();
}
@PostMapping(path = "/add-organizer") // Map ONLY POST Requests
public @ResponseBody String addNewOrganizer(@RequestParam String name, @RequestParam String email) {
Skill9FestOrganizers n = new Skill9FestOrganizers();
n.setName(name);
n.setEmail(email);
organizerRepository.save(n);
return "Organizer added successfully";
}
@GetMapping(path = "/all-eligible")
public @ResponseBody List<Skill9ParticipantDetails> getAllEligible() {
List<Skill9ParticipantDetails> eligible = participantRepository.findByTest1scoreGreaterThan(70);
eligibleRepository.deleteAll();
for (Skill9ParticipantDetails p : eligible) {
Skill9EligibleParticipants s = new Skill9EligibleParticipants();
s.setEmail(p.getEmail());
s.setName(p.getName());
s.setTest1score(p.getTest1score());
s.setTest2score(p.getTest2score());
eligibleRepository.save(s);
}
return eligible;
}
}