Skip to content

Commit bc4a202

Browse files
mikegehardsniemeyer13
authored andcommitted
Add TimeEntry MVC in memory
1 parent 1e08814 commit bc4a202

6 files changed

Lines changed: 231 additions & 0 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ repositories {
99

1010
dependencies {
1111
compile("org.springframework.boot:spring-boot-starter-web")
12+
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.1")
1213

1314
testCompile("org.springframework.boot:spring-boot-starter-test")
1415
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.pivotal.pal.tracker;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
7+
public class InMemoryTimeEntryRepository implements TimeEntryRepository {
8+
private HashMap<Long, TimeEntry> timeEntries = new HashMap<>();
9+
10+
@Override
11+
public TimeEntry create(TimeEntry timeEntry) {
12+
Long id = timeEntries.size() + 1L;
13+
TimeEntry newTimeEntry = new TimeEntry(
14+
id,
15+
timeEntry.getProjectId(),
16+
timeEntry.getUserId(),
17+
timeEntry.getDate(),
18+
timeEntry.getHours()
19+
);
20+
21+
timeEntries.put(id, newTimeEntry);
22+
return newTimeEntry;
23+
}
24+
25+
@Override
26+
public TimeEntry find(Long id) {
27+
return timeEntries.get(id);
28+
}
29+
30+
@Override
31+
public List<TimeEntry> list() {
32+
return new ArrayList<>(timeEntries.values());
33+
}
34+
35+
@Override
36+
public TimeEntry update(Long id, TimeEntry timeEntry) {
37+
TimeEntry updatedEntry = new TimeEntry(
38+
id,
39+
timeEntry.getProjectId(),
40+
timeEntry.getUserId(),
41+
timeEntry.getDate(),
42+
timeEntry.getHours()
43+
);
44+
45+
timeEntries.replace(id, updatedEntry);
46+
return updatedEntry;
47+
}
48+
49+
@Override
50+
public void delete(Long id) {
51+
timeEntries.remove(id);
52+
}
53+
}
54+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
package io.pivotal.pal.tracker;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.SerializationFeature;
6+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
37
import org.springframework.boot.SpringApplication;
48
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
511

612
@SpringBootApplication
713
public class PalTrackerApplication {
814

915
public static void main(String[] args) {
1016
SpringApplication.run(PalTrackerApplication.class, args);
1117
}
18+
19+
@Bean
20+
TimeEntryRepository timeEntryRepository() {
21+
return new InMemoryTimeEntryRepository();
22+
}
23+
24+
@Bean
25+
public ObjectMapper jsonObjectMapper() {
26+
return Jackson2ObjectMapperBuilder.json()
27+
.serializationInclusion(JsonInclude.Include.NON_NULL) // Don’t include null values
28+
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) //ISODate
29+
.modules(new JavaTimeModule())
30+
.build();
31+
}
1232
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.pivotal.pal.tracker;
2+
3+
import java.time.LocalDate;
4+
5+
public class TimeEntry {
6+
private long id;
7+
private long projectId;
8+
private long userId;
9+
private LocalDate date;
10+
private int hours;
11+
12+
public TimeEntry() {
13+
}
14+
15+
public TimeEntry(long projectId, long userId, LocalDate date, int hours) {
16+
this.projectId = projectId;
17+
this.userId = userId;
18+
this.date = date;
19+
this.hours = hours;
20+
}
21+
22+
public TimeEntry(long id, long projectId, long userId, LocalDate date, int hours) {
23+
this.id = id;
24+
this.projectId = projectId;
25+
this.userId = userId;
26+
this.date = date;
27+
this.hours = hours;
28+
}
29+
30+
public long getId() {
31+
return id;
32+
}
33+
34+
public void setId(long id) {
35+
this.id = id;
36+
}
37+
38+
public long getProjectId() {
39+
return projectId;
40+
}
41+
42+
public long getUserId() {
43+
return userId;
44+
}
45+
46+
public LocalDate getDate() {
47+
return date;
48+
}
49+
50+
public int getHours() {
51+
return hours;
52+
}
53+
54+
@Override
55+
public boolean equals(Object o) {
56+
if (this == o) return true;
57+
if (o == null || getClass() != o.getClass()) return false;
58+
59+
TimeEntry timeEntry = (TimeEntry) o;
60+
61+
if (id != timeEntry.id) return false;
62+
if (projectId != timeEntry.projectId) return false;
63+
if (userId != timeEntry.userId) return false;
64+
if (hours != timeEntry.hours) return false;
65+
return date != null ? date.equals(timeEntry.date) : timeEntry.date == null;
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
int result = (int) (id ^ (id >>> 32));
71+
result = 31 * result + (int) (projectId ^ (projectId >>> 32));
72+
result = 31 * result + (int) (userId ^ (userId >>> 32));
73+
result = 31 * result + (date != null ? date.hashCode() : 0);
74+
result = 31 * result + hours;
75+
return result;
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return "TimeEntry{" +
81+
"id=" + id +
82+
", projectId=" + projectId +
83+
", userId=" + userId +
84+
", date='" + date + '\'' +
85+
", hours=" + hours +
86+
'}';
87+
}
88+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.pivotal.pal.tracker;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
import java.util.List;
8+
9+
@RestController
10+
@RequestMapping("/time-entries")
11+
public class TimeEntryController {
12+
13+
private TimeEntryRepository timeEntriesRepo;
14+
15+
public TimeEntryController(TimeEntryRepository timeEntriesRepo) {
16+
this.timeEntriesRepo = timeEntriesRepo;
17+
}
18+
19+
@PostMapping
20+
public ResponseEntity<TimeEntry> create(@RequestBody TimeEntry timeEntry) {
21+
TimeEntry createdTimeEntry = timeEntriesRepo.create(timeEntry);
22+
23+
return new ResponseEntity<>(createdTimeEntry, HttpStatus.CREATED);
24+
}
25+
26+
@GetMapping("{id}")
27+
public ResponseEntity<TimeEntry> read(@PathVariable Long id) {
28+
TimeEntry timeEntry = timeEntriesRepo.find(id);
29+
if (timeEntry != null) {
30+
return new ResponseEntity<>(timeEntry, HttpStatus.OK);
31+
} else {
32+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
33+
}
34+
}
35+
36+
@GetMapping
37+
public ResponseEntity<List<TimeEntry>> list() {
38+
return new ResponseEntity<>(timeEntriesRepo.list(), HttpStatus.OK);
39+
}
40+
41+
@PutMapping("{id}")
42+
public ResponseEntity<TimeEntry> update(@PathVariable Long id, @RequestBody TimeEntry timeEntry) {
43+
TimeEntry updatedTimeEntry = timeEntriesRepo.update(id, timeEntry);
44+
if (updatedTimeEntry != null) {
45+
return new ResponseEntity<>(updatedTimeEntry, HttpStatus.OK);
46+
} else {
47+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
48+
}
49+
}
50+
51+
@DeleteMapping("{id}")
52+
public ResponseEntity<TimeEntry> delete(@PathVariable Long id) {
53+
timeEntriesRepo.delete(id);
54+
55+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
56+
}
57+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.pivotal.pal.tracker;
2+
3+
import java.util.List;
4+
5+
public interface TimeEntryRepository {
6+
TimeEntry create(TimeEntry timeEntry);
7+
TimeEntry find(Long id);
8+
List<TimeEntry> list();
9+
TimeEntry update(Long id, TimeEntry timeEntry);
10+
void delete(Long id);
11+
}

0 commit comments

Comments
 (0)