Pārlūkot izejas kodu

Added products API

master
Archivist pirms 6 gadiem
vecāks
revīzija
dc29dc1936
7 mainītis faili ar 84 papildinājumiem un 9 dzēšanām
  1. +1
    -1
      Dockerfile
  2. +4
    -0
      src/config.cr
  3. +5
    -1
      src/sales_backend.cr
  4. +61
    -1
      src/sales_backend/http/product.cr
  5. +8
    -0
      src/sales_backend/http/user.cr
  6. +3
    -4
      src/sales_backend/invoice.cr
  7. +2
    -2
      src/sales_backend/product.cr

+ 1
- 1
Dockerfile Parādīt failu

@ -5,6 +5,6 @@ COPY . /opt/app/
RUN cd /opt/app && \
shards
RUN cd /opt/app/ && crystal build src/sales_backend.cr
RUN cd /opt/app/ && crystal build --release src/sales_backend.cr
CMD /opt/app/sales_backend

+ 4
- 0
src/config.cr
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu


+ 5
- 1
src/sales_backend.cr Parādīt failu

@ -1,6 +1,10 @@
require "kemal"
require "./sales_backend/http/user"
require "./sales_backend/http/product"
get "/" do |context|
context.response.content_type = "application/json"
"OK".to_json
end
Kemal.run

+ 61
- 1
src/sales_backend/http/product.cr Parādīt failu

@ -2,8 +2,68 @@ require "kemal"
require "../*"
require "io"
require "file"
require "math"
require "exception"
require "crypto/bcrypt/password"
require "dir"
require "uuid"
require "uuid/json"
require "../../config"
require "kemal/param_parser"
require "../../config"
get "/products" do |context|
ret = Array(Product).new
path = Dir.new(Statics.data_path+"products")
sent = 0
skipped = 0
skip = 0
begin
skip_o = context.params.query["skip"]
if skip_o.is_a? String
skip = Int32.from_json skip_o
elsif skip_o.is_a? Int32
skip=skip_o
end
rescue
skip=0
end
limit = Statics.max_product_query
begin
limit_o = Int32.from_json context.params.query["limit"]
if limit_o.is_a? String
limit = Math.min(Int32.from_json(limit_o), limit)
elsif limit_o.is_a? Int32
limit=Math.min(limit_o, Statics.max_product_query)
end
rescue
limit=Statics.max_product_query
end
path.each do |filename|
if(sent<limit)
begin
if filename.char_at(0)!='.'
if(skipped<skip)
skipped+=1
STDOUT.puts "Skipped "+filename
else
ret.push Product.from_json File.read Statics.data_path+"products/"+filename
sent+=1;
STDOUT.puts "Sent "+filename
end
end
rescue exception
end
end
end
ret.to_json
end
post "/products" do |context|
user = authenticate_admin!(context.request.headers["user"],UUID.new(context.request.headers["api_token"]))
product = Product.from_json(context.request.body.not_nil!).not_nil!
File.write Statics.data_path+"products/"+product.id.to_s,product.to_json
"OK".to_json
end

+ 8
- 0
src/sales_backend/http/user.cr Parādīt failu

@ -23,6 +23,14 @@ def authenticate!(user : String, token : UUID) : User
authenticate(user, token).not_nil!
end
def authenticate_admin!(user : String, token : UUID) : User
user = authenticate(user, token).not_nil!
if(user.type==UserType::Administrator)
return user
end
raise "Administrator only"
end
post "/login" do |context|
user = User.from_json context.request.body.not_nil!
user_file = User.from_json File.read(Statics.data_path+"user/"+user.email)

+ 3
- 4
src/sales_backend/invoice.cr Parādīt failu

@ -6,16 +6,15 @@ require "atomic"
require "file"
class Invoice
@@last_invoice : Atomic(Int64)
@@last_invoice : Atomic(Int64) = Atomic(Int64).new(1)
def initialize
@@last_invoice = Atomic(Int64).new(1)
def self.initialize
if(File.exists? Statics.data_path+"invoices_meta.json")
@@last_invoice.set Int64.from_json File.read Statics.data_path+"invoices_meta.json"
end
end
def get_new_id
def self.get_new_id
ret : Int64 =-1
begin
metaf = File.open(Statics.data_path+"invoices_meta.json")

+ 2
- 2
src/sales_backend/product.cr Parādīt failu

@ -6,8 +6,8 @@ class Product
JSON.mapping(
price: {type: Float64, nilable: true},
tax_rate: {type: Float64, nilable: true},
name: {type: String, nilable: true},
description: {type: String, nilable: true},
name: {type: String, default: "Undefined"},
description: {type: String, default: "No description available"},
pic_url: {type: String, nilable: true},
id: UUID,
)

Notiek ielāde…
Atcelt
Saglabāt