help
This commit is contained in:
commit
2a71b28933
3 changed files with 39 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
database.db
|
19
main.pl
Normal file
19
main.pl
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
:- use_module(library(http/http_server)).
|
||||||
|
:- use_module(library(http/http_client)).
|
||||||
|
:- use_module(library(http/http_json)).
|
||||||
|
:- use_module(library(http/json_convert)).
|
||||||
|
:- use_module(library(http/json)).
|
||||||
|
:- use_module(library(persistency)).
|
||||||
|
|
||||||
|
:- initialization
|
||||||
|
http_server([port(8080)]).
|
||||||
|
:- initialization
|
||||||
|
absolute_file_name('database.db', File, [access(write)]),
|
||||||
|
db_attach(File, []).
|
||||||
|
|
||||||
|
:- json_object
|
||||||
|
product(name:atom, price:integer).
|
||||||
|
:- persistent
|
||||||
|
product(name:atom, price:integer).
|
||||||
|
|
||||||
|
:- consult('routes/products.pl').
|
19
routes/products.pl
Normal file
19
routes/products.pl
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
:- http_handler(
|
||||||
|
root(products),
|
||||||
|
products_page,
|
||||||
|
[]
|
||||||
|
).
|
||||||
|
|
||||||
|
% POST /products
|
||||||
|
products_page(Request) :-
|
||||||
|
member(method(post), Request), !,
|
||||||
|
http_read_data(Request, Json, []),
|
||||||
|
json_to_prolog(Json, product(Name, Price)),
|
||||||
|
assert_product(Name, Price),
|
||||||
|
reply_json(Json).
|
||||||
|
|
||||||
|
% GET /products
|
||||||
|
products_page(_Request) :-
|
||||||
|
bagof(product(Name, Price), product(Name, Price), Products),
|
||||||
|
prolog_to_json(Products, Json),
|
||||||
|
reply_json(Json, []).
|
Loading…
Reference in a new issue