Skip to content

Commit

Permalink
Merge pull request #79 from nck974/item-location
Browse files Browse the repository at this point in the history
Item location
  • Loading branch information
nck974 authored Jan 1, 2024
2 parents 0c1ec4c + 60def0b commit d415d89
Show file tree
Hide file tree
Showing 73 changed files with 4,641 additions and 75 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ https://start.spring.io/#!type=maven-project&language=java&platformVersion=3.0.4
#### Create backend docker container

1. First build the image with `cd diogenes && ./mvnw install`.
1. Build the container with `cd .. && docker build -f docker/Dockerfile -t nck974/diogenes:0.0.5 .`
1. Build the container with `cd .. && docker build -f docker/Dockerfile -t nck974/diogenes:0.0.7 .`
1. Generate a token in `https://hub.docker.com` and login with `docker login -u <user>`. Paste the generated token as password.
1. Push the generated container with `docker push nck974/diogenes:0.0.5`.
1. Push the generated container with `docker push nck974/diogenes:0.0.7`.
1. Create the `latest` tag and push it:

```bash
docker tag nck974/diogenes:0.0.5 nck974/diogenes:latest
docker tag nck974/diogenes:0.0.7 nck974/diogenes:latest
docker push nck974/diogenes:latest
```

Expand All @@ -113,13 +113,13 @@ To run the app in development mode just access the folder `diogenes-ng` and star

#### Create frontend docker container

1. Build the container with `docker build -f docker/Dockerfile.angular -t nck974/diogenes-ng:0.0.7 .`
1. Build the container with `docker build -f docker/Dockerfile.angular -t nck974/diogenes-ng:0.0.8 .`
1. Generate a token in `https://hub.docker.com` and login with `docker login -u <user>`. Paste the generated token as password.
1. Push the generated container with `docker push nck974/diogenes-ng:0.0.7`.
1. Push the generated container with `docker push nck974/diogenes-ng:0.0.8`.
1. Create the `latest` tag and push it:

```bash
docker tag nck974/diogenes-ng:0.0.7 nck974/diogenes-ng:latest
docker tag nck974/diogenes-ng:0.0.8 nck974/diogenes-ng:latest
docker push nck974/diogenes-ng:latest
```

Expand Down
6 changes: 3 additions & 3 deletions diogenes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<version>3.2.0</version>
<relativePath />
</parent>
<groupId>dev.nichoko</groupId>
<artifactId>diogenes</artifactId>
<version>0.0.5</version>
<version>0.0.7</version>
<name>diogenes</name>
<description>Software to manage the personal inventary of items that you own.</description>

Expand Down Expand Up @@ -65,7 +65,7 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
<version>42.7.0</version>
</dependency>

<!-- In memory database for unit test -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dev.nichoko.diogenes.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import dev.nichoko.diogenes.model.LocationSummary;
import dev.nichoko.diogenes.model.domain.Location;
import dev.nichoko.diogenes.service.LocationService;
import jakarta.validation.Valid;

@RestController
@RequestMapping("/api/v1/locations")
public class LocationController {
private LocationService locationService;

@Autowired
public LocationController(LocationService locationService) {
this.locationService = locationService;
}

@PostMapping("/")
public ResponseEntity<Location> createLocation(@Valid @RequestBody Location location) {
Location createdLocation = locationService.createLocation(location);
return ResponseEntity.status(HttpStatus.CREATED).body(createdLocation);
}

@GetMapping("/")
public ResponseEntity<List<Location>> getAllLocations() {
return ResponseEntity
.ok(locationService.getAllLocations());
}

@GetMapping("/{id}")
public ResponseEntity<Location> getLocationById(@Valid @PathVariable int id) {
return ResponseEntity.ok(locationService.getLocationById(id));
}

@PutMapping("/{id}")
public ResponseEntity<Location> updateLocation(@Valid @RequestBody Location updateLocation, @PathVariable int id) {
return ResponseEntity.ok().body(locationService.updateLocation(id, updateLocation));
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
public ResponseEntity<Object> deleteLocation(@Valid @PathVariable int id) {
locationService.deleteLocation(id);
return ResponseEntity.noContent().build();
}

@GetMapping("/summary")
public ResponseEntity<List<LocationSummary>> getLocationsSummary() {
return ResponseEntity
.ok(locationService.getLocationsSummary());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.nichoko.diogenes.exception;

public class InvalidLocationException extends RuntimeException {

public InvalidLocationException(int id) {
super("Invalid location with id '" + id + "'.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.nichoko.diogenes.exception;

public class MissingLocationException extends RuntimeException {

public MissingLocationException() {
super("An item needs to have a location assigned.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import dev.nichoko.diogenes.exception.ImageCouldNotBeSavedException;
import dev.nichoko.diogenes.exception.InvalidCategoryException;
import dev.nichoko.diogenes.exception.InvalidLocationException;
import dev.nichoko.diogenes.exception.MissingCategoryException;
import dev.nichoko.diogenes.exception.MissingLocationException;
import dev.nichoko.diogenes.exception.NameAlreadyExistsException;
import dev.nichoko.diogenes.exception.ResourceNotFoundException;
import dev.nichoko.diogenes.exception.UnsupportedImageFormatException;
Expand All @@ -33,12 +35,24 @@ public ErrorResponse handleMissingCategory(MissingCategoryException ex) {
return new ErrorResponse(ex.getMessage());
}

@ExceptionHandler(MissingLocationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleMissingLocation(MissingLocationException ex) {
return new ErrorResponse(ex.getMessage());
}

@ExceptionHandler(InvalidCategoryException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleInvalidCategory(InvalidCategoryException ex) {
return new ErrorResponse(ex.getMessage());
}

@ExceptionHandler(InvalidLocationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleInvalidLocation(InvalidLocationException ex) {
return new ErrorResponse(ex.getMessage());
}

@ExceptionHandler(ImageCouldNotBeSavedException.class)
@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
public ErrorResponse handleImageCouldNotBeSaved(ImageCouldNotBeSavedException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ public class ItemFilter {
private String description;
private Integer number;
private Integer categoryId;
private Integer locationId;

public ItemFilter(String name, String description, Integer number, Integer categoryId) {
public ItemFilter(String name, String description, Integer number, Integer categoryId, Integer locationId) {
this.name = name;
this.description = description;
this.number = number;
this.categoryId = categoryId;
this.locationId = locationId;
}

public String getName() {
Expand All @@ -29,10 +31,14 @@ public Integer getCategoryId() {
return categoryId;
}

public Integer getLocationId() {
return locationId;
}

@Override
public String toString() {
return "ItemFilter [name=" + name + ", description=" + description + ", number=" + number + ", categoryId="
+ categoryId + "]";
+ categoryId + ", locationId=" + locationId + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.nichoko.diogenes.model;

import dev.nichoko.diogenes.model.domain.Location;

public class LocationSummary {
private Location location;
private int itemsNumber;

public LocationSummary(Location location, int itemsNumber) {
this.location = location;
this.itemsNumber = itemsNumber;
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
}

public int getItemsNumber() {
return itemsNumber;
}

public void setItemsNumber(int itemsNumber) {
this.itemsNumber = itemsNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public class Item {
@Transient
private int categoryId;

@OneToOne()
@JoinColumn(name = "location_id")
@JsonProperty(access = Access.READ_ONLY)
private Location location;

@Transient
private int locationId;

public Item() {
}

Expand Down Expand Up @@ -142,6 +150,17 @@ public void setCategory(Category category) {
}
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
if (location != null) {
this.locationId = location.getId();
}
}

public int getCategoryId() {
return categoryId;
}
Expand All @@ -150,6 +169,14 @@ public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}

public int getLocationId() {
return locationId;
}

public void setLocationId(int locationId) {
this.locationId = locationId;
}

public LocalDateTime getUpdatedOn() {
return updatedOn;
}
Expand All @@ -170,7 +197,8 @@ public void setCreatedOn(LocalDateTime createdOn) {
public String toString() {
return "Item [id=" + id + ", name=" + name + ", description=" + description + ", number=" + number
+ ", updatedOn=" + updatedOn + ", createdOn=" + createdOn + ", imagePath=" + imagePath + ", category="
+ category + ", categoryId=" + categoryId + "]";
+ category + ", categoryId=" + categoryId + ", location=" + location + ", locationId=" + locationId
+ "]";
}

}
Loading

0 comments on commit d415d89

Please sign in to comment.