From 7b08fe611fee5ec1c23010fa4cd0ae7eb3542fa3 Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Tue, 25 Jun 2019 16:44:59 +0530 Subject: [PATCH] Feature: SectionPrinter is now working Migration: Section, Printers, SectionPrinters working --- DB/migrate.cmd | 6 +- barker/routes.py | 1 + barker/transactional_view_deriver.py | 21 ++- barker/views/section_printer.py | 52 +++++-- bookie/src/app/app-routing.module.ts | 4 + bookie/src/app/{printers => core}/printer.ts | 0 bookie/src/app/core/section-printer.ts | 17 +++ bookie/src/app/home/home.component.html | 3 + .../printer-detail.component.ts | 2 +- .../printers/printer-list-resolver.service.ts | 2 +- .../printer-list/printer-list-datasource.ts | 2 +- .../printer-list/printer-list.component.ts | 2 +- .../app/printers/printer-resolver.service.ts | 2 +- bookie/src/app/printers/printer.service.ts | 2 +- .../section-printer-datasource.ts | 18 +++ .../section-printer-resolver.service.spec.ts | 15 ++ .../section-printer-resolver.service.ts | 19 +++ .../section-printer.component.css | 3 + .../section-printer.component.html | 65 +++++++++ .../section-printer.component.spec.ts | 25 ++++ .../section-printer.component.ts | 128 ++++++++++++++++++ .../section-printer.service.spec.ts | 15 ++ .../section-printer.service.ts | 50 +++++++ .../section-printers-routing.module.spec.ts | 13 ++ .../section-printers-routing.module.ts | 53 ++++++++ .../section-printers.module.spec.ts | 13 ++ .../section-printers.module.ts | 43 ++++++ 27 files changed, 546 insertions(+), 30 deletions(-) rename bookie/src/app/{printers => core}/printer.ts (100%) create mode 100644 bookie/src/app/core/section-printer.ts create mode 100644 bookie/src/app/section-printers/section-printer-datasource.ts create mode 100644 bookie/src/app/section-printers/section-printer-resolver.service.spec.ts create mode 100644 bookie/src/app/section-printers/section-printer-resolver.service.ts create mode 100644 bookie/src/app/section-printers/section-printer.component.css create mode 100644 bookie/src/app/section-printers/section-printer.component.html create mode 100644 bookie/src/app/section-printers/section-printer.component.spec.ts create mode 100644 bookie/src/app/section-printers/section-printer.component.ts create mode 100644 bookie/src/app/section-printers/section-printer.service.spec.ts create mode 100644 bookie/src/app/section-printers/section-printer.service.ts create mode 100644 bookie/src/app/section-printers/section-printers-routing.module.spec.ts create mode 100644 bookie/src/app/section-printers/section-printers-routing.module.ts create mode 100644 bookie/src/app/section-printers/section-printers.module.spec.ts create mode 100644 bookie/src/app/section-printers/section-printers.module.ts diff --git a/DB/migrate.cmd b/DB/migrate.cmd index 690b51a..db7ff46 100644 --- a/DB/migrate.cmd +++ b/DB/migrate.cmd @@ -16,10 +16,10 @@ call:copyQuery m-Kots "SELECT 'INSERT INTO kots(id, voucher_id, code, food_table call:copyQuery n-Inventories "SELECT 'INSERT INTO inventories(id, kot_id, product_id, sort_order, quantity, price, is_happy_hour, service_charge, is_sc_taxable, service_tax_rate, vat_rate, service_tax_id, vat_id, discount) VALUES (''' + CAST(InventoryID AS Nvarchar(36)) + ''', ' + '''' + CAST(KotID AS Nvarchar(36)) + '''' + ', ' + '''' + CAST(ProductID AS Nvarchar(36)) + '''' + ', ' + CAST(SortOrder AS Nvarchar(36)) + ', ' + CAST(Quantity AS Nvarchar(36)) + ', ' + CAST(Price AS Nvarchar(36)) + ', ' + CASE WHEN IsHappyHour = 1 THEN 'true' ELSE 'false' END + ', ' + CAST(ServiceCharge AS Nvarchar(36)) + ', ' + CASE WHEN IsScTaxable = 1 THEN 'true' ELSE 'false' END + ', ' + CAST(ServiceTaxRate AS Nvarchar(36)) + ', ' + CAST(VatRate AS Nvarchar(36)) + ', ' + '''' + CAST(ServiceTaxID AS Nvarchar(36)) + '''' + ', ' + '''' + CAST(VatID AS Nvarchar(36)) + '''' + ', ' + CAST(Discount AS Nvarchar(36)) + ');' FROM Test.dbo.Inventories;" call:copyQuery o-Modifiers "SELECT 'INSERT INTO modifiers(id, name, show_in_bill, price) VALUES (''' + CAST(ModifierID AS Nvarchar(36)) + ''', ' + '''' + REPLACE(Name, '''', '''''') + '''' + ', ' + CASE WHEN ShowInBill = 1 THEN 'true' ELSE 'false' END + ', ' + CAST(Price AS Nvarchar(36)) + ');' FROM Test.dbo.Modifiers;" call:copyQuery p-InventoryModifiers "SELECT 'INSERT INTO inventory_modifiers(id, inventory_id, modifier_id, price) VALUES (''' + CAST(InventoryModifierID AS Nvarchar(36)) + ''', ' + '''' + CAST(InventoryID AS Nvarchar(36)) + '''' + ', ' + '''' + CAST(ModifierID AS Nvarchar(36)) + '''' + ', 0);' FROM Test.dbo.InventoryModifiers;" -call:copyQuery q-Locations "SELECT 'INSERT INTO locations(id, name) VALUES (''' + CAST(NewID() AS Nvarchar(36)) + ''', ''' + Location + ''');' FROM Test.dbo.PrintLocations GROUP BY Location;" +call:copyQuery q-Sections "SELECT 'INSERT INTO sections(id, name) VALUES (''' + CAST(NewID() AS Nvarchar(36)) + ''', ''' + Location + ''');' FROM Test.dbo.PrintLocations GROUP BY Location;" call:copyQuery r-MachineLocations "SELECT 'INSERT INTO machine_locations(id, machine_name, location_id) SELECT ''' + CAST(MachineLocationID AS Nvarchar(36)) + ''', ' + '''' + REPLACE(Machine, '''', '''''') + '''' + ', id from locations where name = ' + '''' + REPLACE(Location, '''', '''''') + '''' + ';' FROM Test.dbo.MachineLocations;" -call:copyQuery s-Printers "SELECT 'INSERT INTO printers(id, name, cut_code) VALUES (''' + CAST(NewID() AS Nvarchar(36)) + ''', ''' + Printer + ''', ''' + CutCode + ''');' FROM Test.dbo.PrintLocations GROUP BY Printer, CutCode;" -call:copyQuery t-PrintLocations "SELECT 'INSERT INTO print_locations(id, product_group_id, location_id, printer_id, copies) VALUES (''' + CAST(PrintLocationID AS Nvarchar(36)) + ''', ' + COALESCE('''' + CAST(ProductGroupID AS Nvarchar(36)) + '''','null') + ', ' + '(select id from locations where name = ''' + Location + ''')' + ', ' + '(select id from printers where name = ''' + Printer + ''')' + ', ' + CAST(Copies AS nvarchar(36)) + ');' FROM Test.dbo.PrintLocations;" +call:copyQuery s-Printers "SELECT 'INSERT INTO printers(id, name, address, cut_code) VALUES (''' + CAST(NewID() AS Nvarchar(36)) + ''', ''' + Printer + ''', ''' + ''' + Printer + ''', ''' + CutCode + ''');' FROM Test.dbo.PrintLocations GROUP BY Printer, CutCode;" +call:copyQuery t-SectionPrinters "SELECT 'INSERT INTO section_printers(id, menu_category_id, section_id, printer_id, copies) VALUES (''' + CAST(PrintLocationID AS Nvarchar(36)) + ''', ' + COALESCE('''' + CAST(ProductGroupID AS Nvarchar(36)) + '''','null') + ', ' + '(select id from sections where name = ''' + Location + ''')' + ', ' + '(select id from printers where name = ''' + Printer + ''')' + ', ' + CAST(Copies AS nvarchar(36)) + ');' FROM Test.dbo.PrintLocations;" call:copyQuery u-ProductGroupModifiers "SELECT 'INSERT INTO product_group_modifiers(id, product_group_id, modifier_id, show_automatically) VALUES (''' + CAST(ProductGroupModifierID AS Nvarchar(36)) + ''', ' + COALESCE('''' + CAST(ProductGroupID AS Nvarchar(36)) + '''','null') + ', ' + COALESCE('''' + CAST(ModifierID AS Nvarchar(36)) + '''','null') + ', ' + CASE WHEN ShowAutomatically = 1 THEN 'true' ELSE 'false' END + ');' FROM Test.dbo.ProductGroupModifiers;" call:copyQuery v-Reprints "SELECT 'INSERT INTO reprints(id, user_id, date, voucher_id) VALUES (''' + CAST(ReprintID AS Nvarchar(36)) + ''', ' + '''' + CAST(UserID AS Nvarchar(36)) + '''' + ', ' + '''' + CONVERT(Nvarchar(36), Date, 126) + '''' + ', ' + '''' + CAST(VoucherID AS Nvarchar(36)) + '''' + ');' FROM Test.dbo.Reprints;" call:copyQuery w-Settings "SELECT 'INSERT INTO public.settings(id, name, data) VALUES (''' + CAST(SettingID AS Nvarchar(36)) + ''', ' + '''' + REPLACE(Name, '''', '''''') + '''' + ', ' + '''' + REPLACE(Details, '''', '''''') + '''' + ');' FROM Test.dbo.Settings;" diff --git a/barker/routes.py b/barker/routes.py index 638fb17..525f4a3 100644 --- a/barker/routes.py +++ b/barker/routes.py @@ -81,6 +81,7 @@ def includeme(config): config.add_route("v1_sections_list", "/v1/sections") config.add_route("section_printers", "/section-printers/{section_id}") + config.add_route("v1_section_printers_noid", "/v1/section-printers") config.add_route("v1_section_printers", "/v1/section-printers/{section_id}") config.add_route("devices_new", "/devices/new") diff --git a/barker/transactional_view_deriver.py b/barker/transactional_view_deriver.py index 1ef4bbb..51dca5b 100644 --- a/barker/transactional_view_deriver.py +++ b/barker/transactional_view_deriver.py @@ -1,21 +1,30 @@ +import transaction from pyramid.response import Response from sqlalchemy.exc import OperationalError, IntegrityError, DBAPIError -import transaction +from barker.models.validation_exception import ValidationError def transactional_view(view, info): - if info.options.get('trans'): + if info.options.get("trans"): + def wrapper_view(context, request): try: response = view(context, request) except ( - ValueError, KeyError, AttributeError, TypeError, OperationalError, IntegrityError, DBAPIError + ValidationError, + ValueError, + KeyError, + AttributeError, + TypeError, + OperationalError, + IntegrityError, + DBAPIError, ) as ex: transaction.abort() response = Response() response.status_int = 500 - response.content_type = 'application/json' - response.json = {'Error': "Failed validation: {0!s}".format(ex)} + response.content_type = "application/json" + response.json = {"Error": "Failed validation: {0!s}".format(ex)} return response return wrapper_view @@ -26,4 +35,4 @@ def transactional_view(view, info): # return view -transactional_view.options = ('trans',) +transactional_view.options = ("trans",) diff --git a/barker/views/section_printer.py b/barker/views/section_printer.py index 2d9db7a..256c0ce 100644 --- a/barker/views/section_printer.py +++ b/barker/views/section_printer.py @@ -21,15 +21,26 @@ def save(request): json = request.json_body section_id = uuid.UUID(request.matchdict["section_id"]) current = [] - for mcs in json: + for mcs in json["menuCategories"]: menu_category = mcs.get("menuCategory", None) menu_category_id = ( - uuid.UUID(menu_category["id"]) if menu_category is not None else None + uuid.UUID(menu_category["id"]) + if menu_category is not None and menu_category["id"] != "" + else None ) printer = mcs.get("printer", None) - printer_id = uuid.UUID(printer["id"]) if printer is not None else None + printer_id = ( + uuid.UUID(printer["id"]) + if printer is not None + and "id" in printer + and printer["id"] is not None + and printer["id"] != "" + else None + ) if menu_category_id is None and printer_id is None: raise ValidationError("Please choose a default printer") + if printer_id is None: + continue try: copies = int(mcs.get("copies", 0)) if copies < 1: @@ -46,8 +57,8 @@ def save(request): copies=copies, ) .on_conflict_do_update( - constraint=["menu_category_id", "section_id"], - set_=dict(printerid=printer_id, copies=copies), + index_elements=["menu_category_id", "section_id"], + set_=dict(printer_id=printer_id, copies=copies), ) ) request.dbsession.execute(stmt) @@ -61,7 +72,7 @@ def save(request): mark_changed(request.dbsession) transaction.commit() - return report(section_id, request.dbsession) + return {"id": section_id, "menuCategories": report(section_id, request.dbsession)} @view_config( @@ -76,7 +87,7 @@ def delete(request): SectionPrinter.__table__.delete(SectionPrinter.section_id == section_id) mark_changed(request.dbsession) transaction.commit() - return report(section_id, request.dbsession) + return {"id": "", "menuCategories": report(section_id, request.dbsession)} @view_config( @@ -85,20 +96,28 @@ def delete(request): renderer="json", permission="Authenticated", ) +@view_config( + request_method="GET", + route_name="v1_section_printers_noid", + renderer="json", + permission="Authenticated", +) def show_id(request): - section_id = uuid.UUID(request.matchdict["section_id"]) - return report(section_id, request.dbsession) + section_id = request.matchdict.get("section_id", None) + if section_id is not None: + section_id = uuid.UUID(section_id) + return {"id": section_id, "menuCategories": report(section_id, request.dbsession)} @view_config( request_method="GET", route_name="v1_section_printers", renderer="json", - request_param="s", + request_param="m", permission="Authenticated", ) def show_details(request): - section_id = uuid.UUID(request.GET["s"]) + section_id = uuid.UUID(request.matchdict["section_id"]) menu_category_id = request.GET.get("m", None) if menu_category_id is not None: menu_category_id = uuid.UUID(menu_category_id) @@ -115,28 +134,31 @@ def show_details(request): def report(section_id, dbsession): menu_categories = ( - dbsession.query(MenuCategory.id) + dbsession.query(MenuCategory.id, MenuCategory.name) .filter(MenuCategory.is_active == True) .order_by(MenuCategory.sort_order) .all() ) list_ = [] - for item in [None] + menu_categories: + for item in [(None,)] + menu_categories: mc = ( dbsession.query(SectionPrinter) .filter( SectionPrinter.section_id == section_id, - SectionPrinter.menu_category_id == id, + SectionPrinter.menu_category_id == item[0], ) .first() ) list_.append( { - "menuCategory": {"id": item}, + "menuCategory": {"id": "", "name": "Default"} + if item[0] is None + else {"id": item[0], "name": item[1]}, "printer": {} if mc is None else {"id": mc.printer_id}, "copies": 0 if mc is None else mc.copies, } ) + return list_ def section_printer_info(item, dbsession): diff --git a/bookie/src/app/app-routing.module.ts b/bookie/src/app/app-routing.module.ts index ed23740..ad09ac6 100644 --- a/bookie/src/app/app-routing.module.ts +++ b/bookie/src/app/app-routing.module.ts @@ -49,6 +49,10 @@ const routes: Routes = [ path: 'sections', loadChildren: () => import('./sections/sections.module').then(mod => mod.SectionsModule) }, + { + path: 'section-printers', + loadChildren: () => import('./section-printers/section-printers.module').then(mod => mod.SectionPrintersModule) + }, { path: 'tables', loadChildren: () => import('./tables/tables.module').then(mod => mod.TableModule) diff --git a/bookie/src/app/printers/printer.ts b/bookie/src/app/core/printer.ts similarity index 100% rename from bookie/src/app/printers/printer.ts rename to bookie/src/app/core/printer.ts diff --git a/bookie/src/app/core/section-printer.ts b/bookie/src/app/core/section-printer.ts new file mode 100644 index 0000000..55a3008 --- /dev/null +++ b/bookie/src/app/core/section-printer.ts @@ -0,0 +1,17 @@ +import {MenuCategory} from "./menu-category"; +import {Printer} from "./printer"; + +export class SectionPrinter { + id: string; + menuCategories: SectionPrinterItem[]; + + public constructor(init?: Partial) { + Object.assign(this, init); + } +} + +export class SectionPrinterItem { + menuCategory: MenuCategory; + printer: Printer; + copies: number; +} diff --git a/bookie/src/app/home/home.component.html b/bookie/src/app/home/home.component.html index b971385..f019826 100644 --- a/bookie/src/app/home/home.component.html +++ b/bookie/src/app/home/home.component.html @@ -31,6 +31,9 @@ Taxes + + Section Printers + Printers diff --git a/bookie/src/app/printers/printer-detail/printer-detail.component.ts b/bookie/src/app/printers/printer-detail/printer-detail.component.ts index 7a50e9b..c5193fc 100644 --- a/bookie/src/app/printers/printer-detail/printer-detail.component.ts +++ b/bookie/src/app/printers/printer-detail/printer-detail.component.ts @@ -4,7 +4,7 @@ import { ActivatedRoute, Router } from '@angular/router'; import { MatDialog } from "@angular/material/dialog"; import { PrinterService } from '../printer.service'; -import { Printer } from '../printer'; +import { Printer } from '../../core/printer'; import { ToasterService } from '../../core/toaster.service'; import { ConfirmDialogComponent } from "../../shared/confirm-dialog/confirm-dialog.component"; diff --git a/bookie/src/app/printers/printer-list-resolver.service.ts b/bookie/src/app/printers/printer-list-resolver.service.ts index 71eb375..3bd4cc2 100644 --- a/bookie/src/app/printers/printer-list-resolver.service.ts +++ b/bookie/src/app/printers/printer-list-resolver.service.ts @@ -1,6 +1,6 @@ import {Injectable} from '@angular/core'; import {ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot} from '@angular/router'; -import {Printer} from './printer'; +import {Printer} from '../core/printer'; import {Observable} from 'rxjs/internal/Observable'; import {PrinterService} from './printer.service'; diff --git a/bookie/src/app/printers/printer-list/printer-list-datasource.ts b/bookie/src/app/printers/printer-list/printer-list-datasource.ts index ee682a4..15152d8 100644 --- a/bookie/src/app/printers/printer-list/printer-list-datasource.ts +++ b/bookie/src/app/printers/printer-list/printer-list-datasource.ts @@ -1,6 +1,6 @@ import {DataSource} from '@angular/cdk/collections'; import {Observable, of as observableOf} from 'rxjs'; -import {Printer} from '../printer'; +import {Printer} from '../../core/printer'; export class PrinterListDataSource extends DataSource { diff --git a/bookie/src/app/printers/printer-list/printer-list.component.ts b/bookie/src/app/printers/printer-list/printer-list.component.ts index 7776023..3552e65 100644 --- a/bookie/src/app/printers/printer-list/printer-list.component.ts +++ b/bookie/src/app/printers/printer-list/printer-list.component.ts @@ -1,6 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { PrinterListDataSource } from './printer-list-datasource'; -import { Printer } from '../printer'; +import { Printer } from '../../core/printer'; import { ActivatedRoute } from '@angular/router'; @Component({ diff --git a/bookie/src/app/printers/printer-resolver.service.ts b/bookie/src/app/printers/printer-resolver.service.ts index 6144b9a..cc2e2c8 100644 --- a/bookie/src/app/printers/printer-resolver.service.ts +++ b/bookie/src/app/printers/printer-resolver.service.ts @@ -1,7 +1,7 @@ import {Injectable} from '@angular/core'; import {ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot} from '@angular/router'; import {PrinterService} from './printer.service'; -import {Printer} from './printer'; +import {Printer} from '../core/printer'; import {Observable} from 'rxjs/internal/Observable'; @Injectable({ diff --git a/bookie/src/app/printers/printer.service.ts b/bookie/src/app/printers/printer.service.ts index 62bec3a..bf2455e 100644 --- a/bookie/src/app/printers/printer.service.ts +++ b/bookie/src/app/printers/printer.service.ts @@ -3,7 +3,7 @@ import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {ErrorLoggerService} from '../core/error-logger.service'; import {catchError} from 'rxjs/operators'; import {Observable} from 'rxjs/internal/Observable'; -import {Printer} from './printer'; +import {Printer} from '../core/printer'; const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) diff --git a/bookie/src/app/section-printers/section-printer-datasource.ts b/bookie/src/app/section-printers/section-printer-datasource.ts new file mode 100644 index 0000000..88fa68d --- /dev/null +++ b/bookie/src/app/section-printers/section-printer-datasource.ts @@ -0,0 +1,18 @@ +import { DataSource } from '@angular/cdk/collections'; +import { Observable } from 'rxjs'; +import { SectionPrinterItem } from "../core/section-printer"; + + +export class SectionPrinterDataSource extends DataSource { + + constructor(private data: Observable) { + super(); + } + + connect(): Observable { + return this.data; + } + + disconnect() { + } +} diff --git a/bookie/src/app/section-printers/section-printer-resolver.service.spec.ts b/bookie/src/app/section-printers/section-printer-resolver.service.spec.ts new file mode 100644 index 0000000..e5c952d --- /dev/null +++ b/bookie/src/app/section-printers/section-printer-resolver.service.spec.ts @@ -0,0 +1,15 @@ +import {inject, TestBed} from '@angular/core/testing'; + +import {SectionPrinterResolver} from './section-printer-resolver.service'; + +describe('SectionPrinterResolver', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [SectionPrinterResolver] + }); + }); + + it('should be created', inject([SectionPrinterResolver], (service: SectionPrinterResolver) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/bookie/src/app/section-printers/section-printer-resolver.service.ts b/bookie/src/app/section-printers/section-printer-resolver.service.ts new file mode 100644 index 0000000..6325061 --- /dev/null +++ b/bookie/src/app/section-printers/section-printer-resolver.service.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router'; +import { SectionPrinter } from '../core/section-printer'; +import { Observable } from 'rxjs'; +import { SectionPrinterService } from './section-printer.service'; + +@Injectable({ + providedIn: 'root' +}) +export class SectionPrinterResolver implements Resolve { + + constructor(private ser: SectionPrinterService, private router: Router) { + } + + resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { + const id = route.paramMap.get('id'); + return this.ser.get(id); + } +} diff --git a/bookie/src/app/section-printers/section-printer.component.css b/bookie/src/app/section-printers/section-printer.component.css new file mode 100644 index 0000000..82c7afd --- /dev/null +++ b/bookie/src/app/section-printers/section-printer.component.css @@ -0,0 +1,3 @@ +.example-card { + max-width: 400px; +} diff --git a/bookie/src/app/section-printers/section-printer.component.html b/bookie/src/app/section-printers/section-printer.component.html new file mode 100644 index 0000000..a6d616e --- /dev/null +++ b/bookie/src/app/section-printers/section-printer.component.html @@ -0,0 +1,65 @@ +
+ + + Printers for Section + + +
+
+ + Section + + + {{ s.name }} + + + +
+ + + + + + Menu Category + {{row.menuCategory.name}} + + + + + Printer + + + Printer + + -- Default -- + + {{ p.name }} + + + + + + + + + Copies + + + Copies + + + + + + + + +
+
+ + + + +
+
diff --git a/bookie/src/app/section-printers/section-printer.component.spec.ts b/bookie/src/app/section-printers/section-printer.component.spec.ts new file mode 100644 index 0000000..d335dda --- /dev/null +++ b/bookie/src/app/section-printers/section-printer.component.spec.ts @@ -0,0 +1,25 @@ +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; + +import {SectionPrinterComponent} from './section-printer.component'; + +describe('SectionPrinterComponent', () => { + let component: SectionPrinterComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [SectionPrinterComponent] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(SectionPrinterComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/bookie/src/app/section-printers/section-printer.component.ts b/bookie/src/app/section-printers/section-printer.component.ts new file mode 100644 index 0000000..d4f0a08 --- /dev/null +++ b/bookie/src/app/section-printers/section-printer.component.ts @@ -0,0 +1,128 @@ +import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; +import { ActivatedRoute, Router} from '@angular/router'; +import { MatDialog } from '@angular/material/dialog'; +import { FormArray, FormBuilder, FormGroup } from '@angular/forms'; +import { BehaviorSubject, Observable } from "rxjs"; +import { map } from 'rxjs/operators'; +import { SectionPrinter } from '../core/section-printer'; +import { ToasterService } from '../core/toaster.service'; +import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component'; +import { Section } from "../core/section"; +import { Printer } from "../core/printer"; +import { SectionPrinterService } from './section-printer.service'; +import { SectionPrinterDataSource } from "./section-printer-datasource"; + +@Component({ + selector: 'app-section-printer', + templateUrl: './section-printer.component.html', + styleUrls: ['./section-printer.component.css'] +}) +export class SectionPrinterComponent implements OnInit { + @ViewChild('section', {static: true}) sectionElement: ElementRef; + form: FormGroup; + dataSource: SectionPrinterDataSource; + public itemObservable = new BehaviorSubject(new SectionPrinter()); + item: SectionPrinter; + sections: Section[]; + printers: Printer[]; + + displayedColumns = ['menuCategory', 'printer', 'copies']; + + constructor( + private route: ActivatedRoute, + private router: Router, + private fb: FormBuilder, + private toaster: ToasterService, + private dialog: MatDialog, + private ser: SectionPrinterService + ) { + this.createForm(); + } + + createForm() { + this.form = this.fb.group({ + section: '', + menuCategories: this.fb.array([]) + }); + } + + ngOnInit() { + this.route.data + .subscribe((data: { item: SectionPrinter, sections: Section[], printers: Printer[] }) => { + this.sections = data.sections; + this.printers = data.printers; + this.showItem(data.item); + this.dataSource = new SectionPrinterDataSource(this.itemObservable.pipe(map(p => p.menuCategories))); + this.itemObservable.next(this.item); + }); + } + + showItem(item: SectionPrinter) { + this.item = item; + this.form.get('section').setValue(this.item.id); + this.form.setControl('menuCategories', this.fb.array( + this.item.menuCategories.map( + x => this.fb.group({ + menuCategory: x.menuCategory.name, + printer: x.printer.id, + copies: "" + x.copies + }) + ) + )); + } + + + save() { + this.ser.save(this.getItem()) + .subscribe( + (result) => { + this.toaster.show('Success', ''); + }, + (error) => { + this.toaster.show('Danger', error.error); + } + ); + } + + delete() { + this.ser.delete(this.item.id) + .subscribe( + (result) => { + this.toaster.show('Success', ''); + }, + (error) => { + this.toaster.show('Danger', error.error); + } + ); + } + + confirmDelete(): void { + const dialogRef = this.dialog.open(ConfirmDialogComponent, { + width: '250px', + data: {title: 'Delete all printers for this section?', content: 'Are you sure? This cannot be undone.'} + }); + + dialogRef.afterClosed().subscribe((result: boolean) => { + if (result) { + this.delete(); + } + }); + } + + getItem(): SectionPrinter { + const formModel = this.form.value; + this.item.id = formModel.section; + const array = this.form.get('menuCategories') as FormArray; + this.item.menuCategories.forEach((item, index) => { + const cont = array.controls[index].value; + item.printer.id = cont.printer; + item.copies = +cont.copies; + }); + return this.item; + } + + show(val: any) { + this.router.navigate(['/section-printers', val.value]); + } + +} diff --git a/bookie/src/app/section-printers/section-printer.service.spec.ts b/bookie/src/app/section-printers/section-printer.service.spec.ts new file mode 100644 index 0000000..9d364ee --- /dev/null +++ b/bookie/src/app/section-printers/section-printer.service.spec.ts @@ -0,0 +1,15 @@ +import {inject, TestBed} from '@angular/core/testing'; + +import {SectionPrinterService} from './section-printer.service'; + +describe('SectionPrinterService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [SectionPrinterService] + }); + }); + + it('should be created', inject([SectionPrinterService], (service: SectionPrinterService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/bookie/src/app/section-printers/section-printer.service.ts b/bookie/src/app/section-printers/section-printer.service.ts new file mode 100644 index 0000000..1cc8ea1 --- /dev/null +++ b/bookie/src/app/section-printers/section-printer.service.ts @@ -0,0 +1,50 @@ +import {Injectable} from '@angular/core'; +import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; +import {ErrorLoggerService} from '../core/error-logger.service'; +import {catchError} from 'rxjs/operators'; +import {Observable} from 'rxjs/internal/Observable'; +import {SectionPrinter, SectionPrinterItem} from '../core/section-printer'; + +const httpOptions = { + headers: new HttpHeaders({'Content-Type': 'application/json'}) +}; +const url = '/v1/section-printers'; +const serviceName = 'SectionPrinterService'; + +@Injectable({ + providedIn: 'root' +}) +export class SectionPrinterService { + constructor(private http: HttpClient, private log: ErrorLoggerService) { + } + + get(id: string): Observable { + const getUrl: string = (id === null) ? `${url}` : `${url}/${id}`; + return >this.http.get(getUrl) + .pipe( + catchError(this.log.handleError(serviceName, `get id=${id}`)) + ); + } + + item(id: string, menuCategoryId: string): Observable { + const options = {params: new HttpParams().set('m', menuCategoryId)}; + return >this.http.get(`${url}/${id}`, options) + .pipe( + catchError(this.log.handleError(serviceName, 'list')) + ); + } + + save(item: SectionPrinter): Observable { + return >this.http.post(`${url}/${item.id}`, item, httpOptions) + .pipe( + catchError(this.log.handleError(serviceName, 'save')) + ); + } + + delete(id: string): Observable { + return >this.http.delete(`${url}/${id}`, httpOptions) + .pipe( + catchError(this.log.handleError(serviceName, 'delete')) + ); + } +} diff --git a/bookie/src/app/section-printers/section-printers-routing.module.spec.ts b/bookie/src/app/section-printers/section-printers-routing.module.spec.ts new file mode 100644 index 0000000..62b75d2 --- /dev/null +++ b/bookie/src/app/section-printers/section-printers-routing.module.spec.ts @@ -0,0 +1,13 @@ +import { SectionPrintersRoutingModule } from './section-printers-routing.module'; + +describe('SectionPrintersRoutingModule', () => { + let sectionPrintersRoutingModule: SectionPrintersRoutingModule; + + beforeEach(() => { + sectionPrintersRoutingModule = new SectionPrintersRoutingModule(); + }); + + it('should create an instance', () => { + expect(sectionPrintersRoutingModule).toBeTruthy(); + }); +}); diff --git a/bookie/src/app/section-printers/section-printers-routing.module.ts b/bookie/src/app/section-printers/section-printers-routing.module.ts new file mode 100644 index 0000000..55cb14e --- /dev/null +++ b/bookie/src/app/section-printers/section-printers-routing.module.ts @@ -0,0 +1,53 @@ +import {NgModule} from '@angular/core'; +import {CommonModule} from '@angular/common'; +import {RouterModule, Routes} from '@angular/router'; +import {SectionPrinterResolver} from './section-printer-resolver.service'; +import {SectionPrinterComponent} from './section-printer.component'; +import {AuthGuard} from '../auth/auth-guard.service'; +import {SectionListResolver} from "../sections/section-list-resolver.service"; +import {PrinterListResolver} from "../printers/printer-list-resolver.service"; + +const sectionPrinterRoutes: Routes = [ + { + path: '', + component: SectionPrinterComponent, + canActivate: [AuthGuard], + data: { + permission: 'Users' + }, + resolve: { + item: SectionPrinterResolver, + sections: SectionListResolver, + printers: PrinterListResolver + } + }, + { + path: ':id', + component: SectionPrinterComponent, + canActivate: [AuthGuard], + data: { + permission: 'Users' + }, + resolve: { + item: SectionPrinterResolver, + sections: SectionListResolver, + printers: PrinterListResolver + } + } +]; + +@NgModule({ + imports: [ + CommonModule, + RouterModule.forChild(sectionPrinterRoutes) + + ], + exports: [ + RouterModule + ], + providers: [ + SectionPrinterResolver + ] +}) +export class SectionPrintersRoutingModule { +} diff --git a/bookie/src/app/section-printers/section-printers.module.spec.ts b/bookie/src/app/section-printers/section-printers.module.spec.ts new file mode 100644 index 0000000..11df69b --- /dev/null +++ b/bookie/src/app/section-printers/section-printers.module.spec.ts @@ -0,0 +1,13 @@ +import { SectionPrintersModule } from './section-printers.module'; + +describe('SectionPrintersModule', () => { + let sectionPrintersModule: SectionPrintersModule; + + beforeEach(() => { + sectionPrintersModule = new SectionPrintersModule(); + }); + + it('should create an instance', () => { + expect(sectionPrintersModule).toBeTruthy(); + }); +}); diff --git a/bookie/src/app/section-printers/section-printers.module.ts b/bookie/src/app/section-printers/section-printers.module.ts new file mode 100644 index 0000000..fb9e92b --- /dev/null +++ b/bookie/src/app/section-printers/section-printers.module.ts @@ -0,0 +1,43 @@ +import {NgModule} from '@angular/core'; +import {CommonModule} from '@angular/common'; + +import { SectionPrinterComponent } from './section-printer.component'; +import { SectionPrintersRoutingModule } from './section-printers-routing.module'; +import { MatButtonModule } from '@angular/material/button'; +import { MatCardModule } from '@angular/material/card'; +import { MatCheckboxModule } from '@angular/material/checkbox'; +import { MatDividerModule } from '@angular/material/divider'; +import { MatIconModule } from '@angular/material/icon'; +import { MatInputModule } from '@angular/material/input'; +import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; +import { MatSelectModule } from '@angular/material/select'; +import { MatTableModule } from '@angular/material/table'; +import { CdkTableModule } from '@angular/cdk/table'; +import { ReactiveFormsModule } from '@angular/forms'; +import { SharedModule } from '../shared/shared.module'; +import { FlexLayoutModule } from '@angular/flex-layout'; + +@NgModule({ + imports: [ + CommonModule, + CdkTableModule, + FlexLayoutModule, + MatButtonModule, + MatCardModule, + MatCheckboxModule, + MatDividerModule, + MatIconModule, + MatInputModule, + MatProgressSpinnerModule, + MatSelectModule, + MatTableModule, + ReactiveFormsModule, + SharedModule, + SectionPrintersRoutingModule + ], + declarations: [ + SectionPrinterComponent + ] +}) +export class SectionPrintersModule { +}