Skip to content

Commit 7afa369

Browse files
committed
Created CRUD Opearations
1 parent ab58adb commit 7afa369

7 files changed

Lines changed: 254 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
testCompile("org.springframework.boot:spring-boot-starter-test")
1314
}
1415

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
9+
private static HashMap<Long, TimeEntry> sharedData = new HashMap<Long, TimeEntry>();
10+
private Long key= 0L;
11+
12+
13+
@Override
14+
public TimeEntry find(Long id) {
15+
return sharedData.get(id);
16+
}
17+
18+
@Override
19+
public List<TimeEntry> list() {
20+
return new ArrayList<TimeEntry>(sharedData.values());
21+
}
22+
23+
@Override
24+
public TimeEntry update(long id, TimeEntry timeEntry) {
25+
timeEntry.setId(id);
26+
sharedData.put(id, timeEntry);
27+
return timeEntry;
28+
}
29+
30+
@Override
31+
public void delete(long id) {
32+
sharedData.clear();
33+
34+
}
35+
36+
@Override
37+
public TimeEntry create(TimeEntry timeEntry) {
38+
key = key + 1L;
39+
timeEntry.setId(key);
40+
sharedData.put(key, new TimeEntry(timeEntry.getId(), timeEntry.getProjectId(),timeEntry.getUserId(),timeEntry.getDate(),timeEntry.getHours()));
41+
return timeEntry;
42+
}
43+
44+
45+
46+
47+
48+
}

src/main/java/io/pivotal/pal/tracker/PalTrackerApplication.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
7+
8+
import com.fasterxml.jackson.annotation.JsonInclude;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.SerializationFeature;
11+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
512

613

714
@SpringBootApplication
@@ -11,5 +18,20 @@ public static void main(String[] args){
1118
SpringApplication.run(PalTrackerApplication.class, args);
1219

1320
}
21+
22+
@Bean
23+
public TimeEntryRepository TimeEntryRepository(){
24+
return new InMemoryTimeEntryRepository();
25+
26+
}
27+
28+
@Bean
29+
public ObjectMapper jsonObjectMapper() {
30+
return Jackson2ObjectMapperBuilder.json()
31+
.serializationInclusion(JsonInclude.Include.NON_NULL) // Don’t include null values
32+
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) //ISODate
33+
.modules(new JavaTimeModule())
34+
.build();
35+
}
1436

1537
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.pivotal.pal.tracker;
2+
3+
import java.time.LocalDate;
4+
5+
public class TimeEntry {
6+
7+
public TimeEntry(long l, long m, LocalDate parse, int i) {
8+
this.projectId=l;
9+
this.userId=m;
10+
this.date=parse;
11+
this.hours=i;
12+
}
13+
public TimeEntry(long l, long m, long n, LocalDate parse, int i) {
14+
this.id=l;
15+
this.projectId=m;
16+
this.userId=n;
17+
this.date=parse;
18+
this.hours=i;
19+
}
20+
@Override
21+
public String toString() {
22+
return "TimeEntry [id=" + id + ", projectId=" + projectId + ", userId="
23+
+ userId + ", date=" + date + ", hours=" + hours + "]";
24+
}
25+
public TimeEntry() {
26+
// TODO Auto-generated constructor stub
27+
}
28+
29+
private long id;
30+
private long projectId;
31+
private long userId;
32+
private LocalDate date;
33+
private int hours;
34+
35+
public long getId() {
36+
return id;
37+
}
38+
public void setId(long id) {
39+
this.id = id;
40+
}
41+
public long getProjectId() {
42+
return projectId;
43+
}
44+
public void setProjectId(long projectId) {
45+
this.projectId = projectId;
46+
}
47+
public long getUserId() {
48+
return userId;
49+
}
50+
@Override
51+
public int hashCode() {
52+
final int prime = 31;
53+
int result = 1;
54+
result = prime * result + ((date == null) ? 0 : date.hashCode());
55+
result = prime * result + hours;
56+
result = prime * result + (int) (id ^ (id >>> 32));
57+
result = prime * result + (int) (projectId ^ (projectId >>> 32));
58+
result = prime * result + (int) (userId ^ (userId >>> 32));
59+
return result;
60+
}
61+
@Override
62+
public boolean equals(Object obj) {
63+
if (this == obj)
64+
return true;
65+
if (obj == null)
66+
return false;
67+
if (getClass() != obj.getClass())
68+
return false;
69+
TimeEntry other = (TimeEntry) obj;
70+
if (date == null) {
71+
if (other.date != null)
72+
return false;
73+
} else if (!date.equals(other.date))
74+
return false;
75+
if (hours != other.hours)
76+
return false;
77+
if (id != other.id)
78+
return false;
79+
if (projectId != other.projectId)
80+
return false;
81+
if (userId != other.userId)
82+
return false;
83+
return true;
84+
}
85+
public void setUserId(long userId) {
86+
this.userId = userId;
87+
}
88+
public LocalDate getDate() {
89+
return date;
90+
}
91+
public void setDate(LocalDate date) {
92+
this.date = date;
93+
}
94+
public int getHours() {
95+
return hours;
96+
}
97+
public void setHours(int hours) {
98+
this.hours = hours;
99+
}
100+
101+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.pivotal.pal.tracker;
2+
3+
import java.util.List;
4+
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.util.StringUtils;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
@RestController
17+
public class TimeEntryController {
18+
19+
public TimeEntryController(TimeEntryRepository timeEntryRepository) {
20+
this.timeEntryRepository = timeEntryRepository;
21+
}
22+
23+
24+
private TimeEntryRepository timeEntryRepository;
25+
26+
27+
@RequestMapping(value="/time-entries", method = RequestMethod.POST)
28+
public ResponseEntity<TimeEntry> create(@RequestBody TimeEntry timeEntryToCreate) {
29+
TimeEntry createdTimeEntry = timeEntryRepository.create(timeEntryToCreate);
30+
return new ResponseEntity<TimeEntry>(createdTimeEntry, HttpStatus.CREATED);
31+
32+
}
33+
34+
@RequestMapping(value="/time-entries/{itemid}", method = RequestMethod.GET)
35+
public ResponseEntity<TimeEntry> read(@PathVariable("itemid") long l) {
36+
TimeEntry readTimeEntry = timeEntryRepository.find(l);
37+
if(!StringUtils.isEmpty(readTimeEntry))
38+
return new ResponseEntity<TimeEntry>(readTimeEntry, HttpStatus.OK);
39+
else
40+
return new ResponseEntity<TimeEntry>(readTimeEntry, HttpStatus.NOT_FOUND);
41+
42+
}
43+
44+
@RequestMapping(value="/time-entries", method = RequestMethod.GET)
45+
public ResponseEntity<List<TimeEntry>> list() {
46+
List<TimeEntry> listTimeEntry = timeEntryRepository.list();
47+
if(!StringUtils.isEmpty(listTimeEntry))
48+
return new ResponseEntity<List<TimeEntry>>(listTimeEntry, HttpStatus.OK);
49+
else
50+
return new ResponseEntity<List<TimeEntry>>(listTimeEntry, HttpStatus.NOT_FOUND);
51+
}
52+
53+
@RequestMapping(value="/time-entries/{itemid}", method = RequestMethod.PUT)
54+
public ResponseEntity update(@PathVariable("itemid") long l, @RequestBody TimeEntry expected) {
55+
TimeEntry updatedTimeEntry = timeEntryRepository.update(l, expected);
56+
if(!StringUtils.isEmpty(updatedTimeEntry))
57+
return new ResponseEntity<TimeEntry>(updatedTimeEntry, HttpStatus.OK);
58+
else
59+
return new ResponseEntity<TimeEntry>(updatedTimeEntry, HttpStatus.NOT_FOUND);
60+
}
61+
62+
@RequestMapping(value="/time-entries/{itemid}", method = RequestMethod.DELETE)
63+
public ResponseEntity<TimeEntry> delete(@PathVariable("itemid") long l) {
64+
timeEntryRepository.delete(l);
65+
return new ResponseEntity<TimeEntry>(HttpStatus.NO_CONTENT);
66+
}
67+
68+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.pivotal.pal.tracker;
2+
3+
import java.util.List;
4+
5+
6+
public interface TimeEntryRepository {
7+
TimeEntry find(Long id);
8+
List<TimeEntry> list();
9+
TimeEntry update(long id, TimeEntry timeEntry);
10+
void delete(long id);
11+
TimeEntry create(TimeEntry any);
12+
13+
}

src/test/java/test/pivotal/pal/tracker/InMemoryTimeEntryRepositoryTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void delete() throws Exception {
6666
TimeEntry created = repo.create(new TimeEntry(123L, 456L, LocalDate.parse("2017-01-08"), 8));
6767

6868
repo.delete(created.getId());
69+
6970
assertThat(repo.list()).isEmpty();
7071
}
7172
}

0 commit comments

Comments
 (0)