-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- creation of "myproduct" db and "products" table in the dockerized postegres db using the file ~/docker/sql/supermarketProducts.sql - "myproducts" db is binded at port 8080 of localhost. - creation of two simple endpoint *localhost:8081/products *localhost:8081/products?id=[int] that return the information stored in "products" table
- Loading branch information
1 parent
a46fa5e
commit 0c44e99
Showing
14 changed files
with
345 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"sbt","version":"1.7.1","bspVersion":"2.0.0-M5","languages":["scala"],"argv":["/Users/giovannimorlin/Library/Java/JavaVirtualMachines/corretto-1.8.0_332/Contents/Home/jre/bin/java","-Xms100m","-Xmx100m","-classpath","/Users/giovannimorlin/Library/Application Support/JetBrains/IdeaIC2022.2/plugins/Scala/launcher/sbt-launch.jar","xsbt.boot.Boot","-bsp","--sbt-launch-jar=/Users/giovannimorlin/Library/Application%20Support/JetBrains/IdeaIC2022.2/plugins/Scala/launcher/sbt-launch.jar"]} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# supermaket | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,65 @@ | ||
ThisBuild / version := "0.1.0-SNAPSHOT" | ||
version := "0.1.0-SNAPSHOT" | ||
|
||
scalaVersion :="2.13.8" | ||
|
||
val DoobieVersion = "1.0.0-RC2" | ||
|
||
val NewTypeVersion = "0.4.4" | ||
|
||
//akka-actor, akka-stream, .. need the same version | ||
val akkaVersion = "2.6.8" | ||
|
||
//akka-http, is an extrenal like alpakka, persistance .. | ||
// isn't constraint to have the same vesion as core module | ||
val akkaHttpVersion = "10.2.10" | ||
|
||
val scalaTestVersion = "3.2.13" | ||
|
||
val circeVersion = "0.14.1" | ||
|
||
val http4sVersion = "0.23.12" | ||
|
||
|
||
libraryDependencies ++= Seq( | ||
"org.tpolecat" %% "doobie-core" % DoobieVersion, | ||
"org.tpolecat" %% "doobie-postgres" % DoobieVersion, | ||
"org.tpolecat" %% "doobie-hikari" % DoobieVersion, | ||
"io.estatico" %% "newtype" % NewTypeVersion, | ||
|
||
"com.github.nscala-time" %% "nscala-time" % "2.30.0", | ||
|
||
//actor | ||
"com.typesafe.akka" %% "akka-actor-typed" % akkaVersion, | ||
"com.typesafe.akka" %% "akka-stream" % akkaVersion, | ||
"com.typesafe.akka" %% "akka-testkit" % akkaVersion, | ||
// akka http | ||
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion, | ||
"com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion, | ||
"com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion, | ||
|
||
"org.scalatest" %% "scalatest" % scalaTestVersion, | ||
|
||
"io.circe" %% "circe-core" % circeVersion, | ||
"io.circe" %% "circe-generic" % circeVersion, | ||
"io.circe" %% "circe-parser" % circeVersion, | ||
|
||
"org.http4s" %% "http4s-dsl" % http4sVersion, | ||
"org.http4s" %% "http4s-blaze-server" % http4sVersion, | ||
"org.http4s" %% "http4s-blaze-client" % http4sVersion, | ||
|
||
) | ||
|
||
//scalacOptions ++= Seq ("-Ypartial-unification" ) | ||
|
||
//versione non giusta :( (prova la versione sotto 2, cit Luca) | ||
//addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.1") | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
ThisBuild / scalaVersion := "2.13.8" | ||
|
||
lazy val root = (project in file(".")) | ||
.settings( | ||
name := "supermaket" | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: '3.1' | ||
|
||
services: | ||
db: | ||
image: postgres | ||
restart: always | ||
volumes: | ||
- "./sql:/docker-entrypoint-initdb.d" | ||
environment: | ||
- "POSTGRES_USER=docker" | ||
- "POSTGRES_PASSWORD=docker" | ||
ports: | ||
- "5432:5432" | ||
adminer: | ||
image: adminer | ||
restart: always | ||
ports: | ||
- 8080:8080 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- Database | ||
CREATE DATABASE myproducts; | ||
\c myproducts; | ||
|
||
CREATE TABLE products ( | ||
id int NOT NULL, | ||
name character varying NOT NULL, | ||
brand character varying NOT NULL, | ||
brandProducer character varying NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO products (id, name, brand, brandProducer) VALUES | ||
('1', 'Nutella', 'Ferrero', 'Ferrero'); | ||
INSERT INTO products (id, name, brand, brandProducer) VALUES | ||
('2', 'San Benedetto', 'Fonte essenziale', 'Nestle'); | ||
INSERT INTO products (id, name, brand, brandProducer) VALUES | ||
('3', 'Patate Lays', 'Patate azienda', 'Patate Holding'); | ||
COMMIT; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version = 1.7.1 | ||
sbt.version = 1.2.8 |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import akka.actor.typed.ActorSystem | ||
import akka.actor.typed.scaladsl.Behaviors | ||
import akka.http.scaladsl.Http | ||
import service.ProductService | ||
|
||
object Main extends App { | ||
|
||
implicit val system = ActorSystem(Behaviors.empty, "supermarket") | ||
implicit val ec = system.executionContext | ||
|
||
Http().newServerAt("localhost",8081).bind(ProductService.ServerRoute) | ||
|
||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package model | ||
|
||
import doobie.{Read, Write} | ||
import spray.json.DefaultJsonProtocol | ||
|
||
|
||
case class ProductId(id:Int) | ||
case class ProductName(name:String) | ||
case class ProductBrand(brand:String) | ||
case class ProductBrandProducer(brandproducer:String) | ||
|
||
case class Product(id: ProductId, | ||
name: ProductName, | ||
brand: ProductBrand, | ||
brandproducer: ProductBrandProducer, | ||
//expirationDate:DateTime, | ||
//aislePosition: Int, | ||
//rackAisle: Int, | ||
//shelfRack: Int, | ||
//category: String, | ||
//nutritionTable:Option[String] | ||
) | ||
|
||
trait ProductJsonProtocol extends DefaultJsonProtocol { | ||
implicit val productIdFormat = jsonFormat1(ProductId) | ||
implicit val productNameFormat = jsonFormat1(ProductName) | ||
implicit val productBrandFormat = jsonFormat1(ProductBrand) | ||
implicit val productBrandProducerFormat = jsonFormat1(ProductBrandProducer) | ||
|
||
//difficoltà: | ||
//non riesce a risolere la case class quando c'è il companion obj | ||
//ho risolto inglobando gli impliciti def nel companion obj | ||
implicit val productFormat = jsonFormat4(Product) | ||
|
||
//necessari per doobie | ||
implicit val productRead: Read[Product] = | ||
Read[(Int, String, String, String)].map { | ||
case (id, name, brand, brandproducer) => | ||
Product(ProductId(id), | ||
ProductName(name), | ||
ProductBrand(brand), | ||
ProductBrandProducer(brandproducer)) | ||
} | ||
|
||
implicit val productWrite: Write[Product] = | ||
Write[(Int, String, String, String)].contramap { | ||
case Product(ProductId(id), | ||
ProductName(name), | ||
ProductBrand(brand), | ||
ProductBrandProducer(brandproducer)) => (id, name, brand, brandproducer) | ||
|
||
} | ||
} | ||
|
||
|
||
/* | ||
case class BrandProducer(id: String, | ||
// date: DateTime, | ||
producer: String) | ||
case class AisleWharehouse() | ||
case class AisleMarket(id: String, | ||
// dateOfLastChange: DateTime, | ||
rackAisleMarket: List[RackAisleMarket]) | ||
case class RackAisleMarket(id: String, | ||
// date: DateTime, | ||
shelfRackAisleMarket: List[ShelfRackAisleMarket]) | ||
case class ShelfRackAisleMarket(id: String, | ||
// date: DateTime, | ||
positionInRack: Int, | ||
product:List[Product])// formata da aisle+rack | ||
case class MarketName(id: String, | ||
// date: DateTime, | ||
name: String, | ||
address: String, | ||
aisleMarket: List[AisleMarket], | ||
productCategory: List[String], | ||
categoryToAisle: Map[AisleMarket,List[String]]) | ||
case class ProductPosition(category: String, | ||
MarketId: String) | ||
*/ | ||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"id": "kjnnjjk", | ||
"name": "jnkjjn", | ||
"expirationDate": "20-8-2020", | ||
"brand": "kjnk", | ||
"brandProducer": "jjnkn", | ||
"aislePosition": 1, | ||
"rackAisle": 2, | ||
"shelfRack": 3, | ||
"category": "kjjkn" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package service | ||
|
||
|
||
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, StatusCodes} | ||
import akka.http.scaladsl.server.Directives._ | ||
import akka.http.scaladsl.server.Route | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
|
||
import doobie.implicits._ | ||
import doobie.util.transactor.Transactor | ||
|
||
import spray.json.enrichAny | ||
|
||
import model.{Product, ProductJsonProtocol} | ||
|
||
|
||
|
||
object ProductService extends ProductJsonProtocol { | ||
|
||
val xa: Transactor[IO] = Transactor.fromDriverManager[IO]( | ||
"org.postgresql.Driver", | ||
"jdbc:postgresql:myproducts", | ||
"docker", | ||
"docker" | ||
) | ||
|
||
def openPrintIO[A](opIO: IO[A]): IO[Unit] = | ||
opIO.map { x => | ||
print(x) | ||
} | ||
|
||
|
||
val ServerRoute: Route = | ||
path("products") { | ||
//localhost:8081/products?id=1 | ||
parameter("id".as[Int]) { id => | ||
get { | ||
val IOqueryDB = | ||
sql"select * from products where id=$id" | ||
.query[Product] | ||
.option | ||
.transact(xa) | ||
|
||
val r = IOqueryDB.unsafeRunSync() match { | ||
case x: Option[Product] => | ||
HttpEntity(ContentTypes.`application/json`, x.toJson.prettyPrint) | ||
} | ||
|
||
//openPrintIO(IOqueryDB).unsafeRunSync() | ||
complete(r) | ||
} | ||
} ~ | ||
//localhost:8081/products | ||
get { | ||
val IOqueryDB = | ||
sql"select * from products" | ||
.query[Product] | ||
.to[List] | ||
.transact(xa) | ||
|
||
val r = IOqueryDB.unsafeRunSync() match { | ||
case x: List[Product] => | ||
HttpEntity(ContentTypes.`application/json`, x.toJson.prettyPrint) | ||
//ritorna un list of json!! | ||
} | ||
|
||
//openPrintIO(IOqueryDB).unsafeRunSync() | ||
complete(r) | ||
} | ||
} | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"id0": { | ||
"id" : "kjnnjjk", | ||
"name" : "jnkjjn", | ||
"expirationDate":"20-8-2020", | ||
"brand": "kjnk", | ||
"brandProducer" : "jjnkn", | ||
"aislePosition" : 1, | ||
"rackAisle" : 2, | ||
"shelfrack" : 3, | ||
"category" : "kjjkn", | ||
"nutritionTable" : "" | ||
}, | ||
"id1" : { | ||
"id" : "kjnnjjk", | ||
"name" : "jnkjjn", | ||
"expirationDate":"20-8-2020", | ||
"brand": "kjnk", | ||
"brandProducer" : "jjnkn", | ||
"aislePosition" : 4, | ||
"rackAisle" : 3, | ||
"shelfrack" : 3, | ||
"category" : "kjjkn", | ||
"nutritionTable" : "" | ||
}, | ||
"id3": { | ||
"id" : "kjnnjjk", | ||
"name" : "jnkjjn", | ||
"expirationDate":"20-8-2020", | ||
"brand": "kjnk", | ||
"brandProducer" : "jjnkn", | ||
"aislePosition" : 1, | ||
"rackAisle" : 2, | ||
"shelfrack" : 5, | ||
"category" : "kjjkn", | ||
"nutritionTable" : "" | ||
}, | ||
"id4" : { | ||
"id" : "kjnnjjk", | ||
"name" : "jnkjjn", | ||
"expirationDate":"20-8-2020", | ||
"brand": "kjnk", | ||
"brandProducer" : "jjnkn", | ||
"aislePosition" : 4, | ||
"rackAisle" : 3, | ||
"shelfrack" : 6, | ||
"category" : "kjjkn", | ||
"nutritionTable" : "" | ||
} | ||
} |