// NFLApiController.java
package com.nighthawk.spring_portfolio.mvc.nfl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
@RestController
@RequestMapping("/api/nfl")
public class NFLApiController {
@Autowired
private NFLJpaRepository repository;
@GetMapping("/")
public ResponseEntity<List<NFL>> getNFLPlayers() {
return new ResponseEntity<>(repository.findAllByOrderByNameAsc(), HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<NFL> getNFLPlayer(@PathVariable long id) {
NFL player = repository.findById(id).orElse(null);
if (player != null) {
return new ResponseEntity<>(player, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<NFL> deleteNFLPlayer(@PathVariable long id) {
NFL player = repository.findById(id).orElse(null);
if (player != null) {
repository.deleteById(id);
return new ResponseEntity<>(player, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
@PostMapping("/post")
public ResponseEntity<Object> postNFLPlayer(@RequestParam("name") String name,
@RequestParam("position") String position,
@RequestParam("height") float height,
@RequestParam("dob") String dobString) {
Date dob;
try {
dob = new SimpleDateFormat("MM-dd-yyyy").parse(dobString);
} catch (Exception e) {
return new ResponseEntity<>(dobString + " error; try MM-dd-yyyy", HttpStatus.BAD_REQUEST);
}
NFL player = new NFL(name, position, height, dob);
repository.save(player);
return new ResponseEntity<>(name + " is created successfully", HttpStatus.CREATED);
}
@PostMapping("/search")
public ResponseEntity<Object> nflPlayerSearch(@RequestBody final NFLSearchRequest searchRequest) {
String term = searchRequest.getTerm();
List<NFL> list = repository.findByNameContainingIgnoreCaseOrPositionContainingIgnoreCase(term, term);
return new ResponseEntity<>(list, HttpStatus.OK);
}
}
Extra Credit - NBA API with JWT Security Features
NBA.java
// NBA.java
package com.nighthawk.spring_portfolio.mvc.nba;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
import org.springframework.format.annotation.DateTimeFormat;
import com.vladmihalcea.hibernate.type.json.JsonType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "nba_players")
@Convert(attributeName = "nba", converter = JsonType.class)
public class NBA {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NonNull
@Size(min = 2, max = 30, message = "Name (2 to 30 chars)")
private String name;
@NonNull
private String position;
private float height;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dob;
@JdbcTypeCode(SqlTypes.JSON)
@Column(columnDefinition = "jsonb")
private Map<String, Map<String, Object>> stats = new HashMap<>();
public NBA(String name, String position, float height, Date dob) {
this.name = name;
this.position = position;
this.height = height;
this.dob = dob;
}
public int getAge() {
if (this.dob != null) {
LocalDate birthDay = this.dob.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return Period.between(birthDay, LocalDate.now()).getYears();
}
return -1;
}
public static NBA[] init() {
NBA player1 = new NBA();
player1.setName("LeBron James");
player1.setPosition("Forward");
player1.setHeight(6.9f);
try {
Date d = new SimpleDateFormat("MM-dd-yyyy").parse("12-30-1984");
player1.setDob(d);
} catch (Exception e) {
}
NBA player2 = new NBA();
player2.setName("Stephen Curry");
player2.setPosition("Guard");
player2.setHeight(6.3f);
try {
Date d = new SimpleDateFormat("MM-dd-yyyy").parse("03-14-1988");
player2.setDob(d);
} catch (Exception e) {
}
NBA players[] = { player1, player2 };
return players;
}
public static void main(String[] args) {
NBA players[] = init();
for (NBA player : players) {
System.out.println(player);
}
}
}
NBAApiController.java
// NBAApiController.java
package com.nighthawk.spring_portfolio.mvc.nba;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
@RestController
@RequestMapping("/api/nba")
public class NBAApiController {
@Autowired
private NBAJpaRepository repository;
@GetMapping("/")
public ResponseEntity<List<NBA>> getNBAPlayers() {
return new ResponseEntity<>(repository.findAllByOrderByNameAsc(), HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<NBA> getNBAPlayer(@PathVariable long id) {
NBA player = repository.findById(id).orElse(null);
if (player != null) {
return new ResponseEntity<>(player, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<NBA> deleteNBAPlayer(@PathVariable long id) {
NBA player = repository.findById(id).orElse(null);
if (player != null) {
repository.deleteById(id);
return new ResponseEntity<>(player, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
@PostMapping("/post")
public ResponseEntity<Object> postNBAPlayer(@RequestParam("name") String name,
@RequestParam("position") String position,
@RequestParam("height") float height,
@RequestParam("dob") String dobString) {
Date dob;
try {
dob = new SimpleDateFormat("MM-dd-yyyy").parse(dobString);
} catch (Exception e) {
return new ResponseEntity<>(dobString + " error; try MM-dd-yyyy", HttpStatus.BAD_REQUEST);
}
NBA player = new NBA(name, position, height, dob);
repository.save(player);
return new ResponseEntity<>(name + " is created successfully", HttpStatus.CREATED);
}
@PostMapping("/search")
public ResponseEntity<Object> nbaPlayerSearch(@RequestBody final NBASearchRequest searchRequest) {
String term = searchRequest.getTerm();
List<NBA> list = repository.findByNameContainingIgnoreCaseOrPositionContainingIgnoreCase(term, term);
return new ResponseEntity<>(list, HttpStatus.OK);
}
}
Shivansh's HACKS
SHIVANSH
Popcorn HACKS
Popcorn HACKS
Poster
Postman Screenshots
My API - NFL Repository with JWT Features
NFL.java
NFLApiController.java
Extra Credit - NBA API with JWT Security Features
NBA.java
NBAApiController.java