From 83b619536a1313eaa7dba17b7106519434a5c225 Mon Sep 17 00:00:00 2001 From: Archivist Date: Mon, 9 Jul 2018 23:04:34 +0200 Subject: [PATCH] Made atomic and disk stored invoice numbers (UNTESTED) --- src/sales_backend/address.cr | 4 ++-- src/sales_backend/http/product.cr | 9 +++++++++ src/sales_backend/invoice.cr | 30 +++++++++++++++++++++++++++++- src/sales_backend/product.cr | 5 +++-- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/sales_backend/http/product.cr diff --git a/src/sales_backend/address.cr b/src/sales_backend/address.cr index 2401971..5520bfc 100644 --- a/src/sales_backend/address.cr +++ b/src/sales_backend/address.cr @@ -19,7 +19,7 @@ class Address country: Country, is_default: {type: Bool, default: false}, ) - def == (other : Address) - self.to_json==other.to_json + def <=> (other : Address) + self.to_json<=>other.to_json end end \ No newline at end of file diff --git a/src/sales_backend/http/product.cr b/src/sales_backend/http/product.cr new file mode 100644 index 0000000..6f58e43 --- /dev/null +++ b/src/sales_backend/http/product.cr @@ -0,0 +1,9 @@ +require "kemal" +require "../*" +require "io" +require "file" +require "exception" +require "crypto/bcrypt/password" +require "uuid" +require "uuid/json" +require "../../config" \ No newline at end of file diff --git a/src/sales_backend/invoice.cr b/src/sales_backend/invoice.cr index 0fee2f7..046e6bb 100644 --- a/src/sales_backend/invoice.cr +++ b/src/sales_backend/invoice.cr @@ -1,11 +1,39 @@ require "json" require "./invoice_line" require "./address" +require "../config" +require "atomic" +require "file" class Invoice + @@last_invoice : Atomic(Int64) + + def initialize + @@last_invoice = Atomic(Int64).new(1) + 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 + ret : Int64 =-1 + begin + metaf = File.open(Statics.data_path+"invoices_meta.json") + metaf.flock_exclusive do + metaf.truncate + ret = @@last_invoice.add(1) + metaf.write @@last_invoice.get.to_json + end + ensure + metaf.close + end + return ret + end + JSON.mapping( items: Array(InvoiceLine), invoicing_address: Address, - delivery_address: Address + delivery_address: Address, + id: Int64 ) end \ No newline at end of file diff --git a/src/sales_backend/product.cr b/src/sales_backend/product.cr index dca779e..edce325 100644 --- a/src/sales_backend/product.cr +++ b/src/sales_backend/product.cr @@ -1,5 +1,6 @@ require "json" - +require "uuid" +require "uuid/json" class Product JSON.mapping( @@ -8,6 +9,6 @@ class Product name: {type: String, nilable: true}, description: {type: String, nilable: true}, pic_url: {type: String, nilable: true}, - id: Int64, + id: UUID, ) end \ No newline at end of file