generated from nighthawkcoders/spring_portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalendarApiController.java
90 lines (73 loc) · 4 KB
/
CalendarApiController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.nighthawk.spring_portfolio.mvc.calendar;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/** Calendar API
* Calendar Endpoint: /api/calendar/isLeapYear/2022, Returns: {"year":2020,"isLeapYear":false}
*/
@RestController
@RequestMapping("/api/calendar")
public class CalendarApiController {
/** GET isLeapYear endpoint
* ObjectMapper throws exceptions on bad JSON
* @throws JsonProcessingException
* @throws JsonMappingException
*/
@GetMapping("/isLeapYear/{year}")
public ResponseEntity<JsonNode> getIsLeapYear(@PathVariable int year) throws JsonMappingException, JsonProcessingException {
// Backend Year Object
Year year_obj = new Year();
year_obj.setYear(year); // evaluates Leap Year
// Turn Year Object into JSON
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(year_obj.isLeapYearToString()); // this requires exception handling
return ResponseEntity.ok(json); // JSON response, see ExceptionHandlerAdvice for throws
}
@GetMapping("/firstDayOfYear/{year}")
public ResponseEntity<JsonNode> getfirstDayOfYear(@PathVariable int year) throws JsonMappingException, JsonProcessingException {
// Backend Year Object
Year year_obj = new Year();
year_obj.setYear(year); // evaluates Leap Year
// Turn Year Object into JSON
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(year_obj.firstDayOfYearToString()); // this requires exception handling
return ResponseEntity.ok(json); // JSON response, see ExceptionHandlerAdvice for throws
}
@GetMapping("/dayOfYear/{month}/{day}/{year}")
public ResponseEntity<JsonNode> getdayOfYear(@PathVariable int month, @PathVariable int day, @PathVariable int year) throws JsonMappingException, JsonProcessingException {
// Backend Year Object
Year year_obj = new Year();
year_obj.setdayOfYear(month, day, year); // evaluates Leap Year
// Turn Year Object into JSON
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(year_obj.dayOfYearToString()); // this requires exception handling
return ResponseEntity.ok(json); // JSON response, see ExceptionHandlerAdvice for throws
}
@GetMapping("/numberOfLeapYears/{year1}/{year2}")
public ResponseEntity<JsonNode> getnumberOfLeapYears(@PathVariable int year1, @PathVariable int year2) throws JsonMappingException, JsonProcessingException {
// Backend Year Object
Year year_obj = new Year();
year_obj.setnumberOfLeapYears(year1, year2); // evaluates Leap Year
// Turn Year Object into JSON
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(year_obj.numberOfLeapYearsToString()); // this requires exception handling
return ResponseEntity.ok(json); // JSON response, see ExceptionHandlerAdvice for throws
}
@GetMapping("/dayOfWeek/{month}/{day}/{year}")
public ResponseEntity<JsonNode> dayOfWeek(@PathVariable int month, @PathVariable int day, @PathVariable int year) throws JsonMappingException, JsonProcessingException {
// Backend Year Object
Year year_obj = new Year();
year_obj.setdayOfWeek(month, day, year); // evaluates Leap Year
// Turn Year Object into JSON
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(year_obj.dayOfWeekToString()); // this requires exception handling
return ResponseEntity.ok(json); // JSON response, see ExceptionHandlerAdvice for throws
}
// add other methods
}