This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package za.co.dubedivine.groceryapp.controller | |
import org.springframework.http.HttpStatus | |
import org.springframework.http.ResponseEntity | |
import org.springframework.web.bind.annotation.* | |
import za.co.dubedivine.groceryapp.model.GroceryItem | |
import za.co.dubedivine.groceryapp.model.responseModel.StatusResponseEntity | |
import za.co.dubedivine.groceryapp.repository.GroceryItemRepository | |
@RestController |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package za.co.dubedivine.groceryapp.controller | |
import org.springframework.web.bind.annotation.* | |
@RestController | |
@RequestMapping("groceries") | |
class GroceriesController(private val groceryRepository: GroceryItemRepository) { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "Beans", | |
"id": 1, | |
"available": true | |
}, | |
{ | |
"name": "Eggs", | |
"id": 2, | |
"available": false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package za.co.dubedivine.groceryapp.model | |
import javax.persistence.Entity | |
import javax.persistence.GeneratedValue | |
import javax.persistence.GenerationType | |
import javax.persistence.Id | |
@Entity | |
data class GroceryItem(var name: String, var isAvailable: Boolean) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package za.co.dubedivine.groceryapp.repository | |
import org.springframework.data.repository.CrudRepository | |
import org.springframework.stereotype.Repository | |
import za.co.dubedivine.groceryapp.model.GroceryItem | |
@Repository | |
interface GroceryItemRepository: CrudRepository<GroceryItem, Long> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Query("select g from GroceryItem g where g.isAvailable = ?1") | |
fun findItemsByAvailability(availability: Boolean): List<GroceryItem> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GetMapping | |
fun getGroceriesList(): MutableIterable<GroceryItem> { | |
return groceryRepository.findAll() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PutMapping | |
fun addGroceryItem(@RequestBody groceryItem: GroceryItem): ResponseEntity<StatusResponseEntity<GroceryItem>> { | |
val savedItem = groceryRepository.save(groceryItem) | |
return ResponseEntity(StatusResponseEntity( | |
true, | |
"Added new grocery item to your list", | |
savedItem | |
), HttpStatus.CREATED) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package za.co.dubedivine.groceryapp.model.responseModel | |
data class StatusResponseEntity<T>(val status: Boolean, val message: String, val entity: T?) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package za.co.dubedivine.groceryapp.config | |
import org.springframework.boot.CommandLineRunner | |
import org.springframework.stereotype.Component | |
import za.co.dubedivine.groceryapp.model.GroceryItem | |
import za.co.dubedivine.groceryapp.repository.GroceryItemRepository | |
import javax.persistence.PrePersist | |
@Component | |
class DBHelper(private val groceryRepository: GroceryItemRepository) : CommandLineRunner { |
OlderNewer