/**
 * Created by ngerakines on 4/11/16.
 */ import { Logger } from "bunyan" ; import * as express from "express" ; import { StorageManager } from "./storage" ; export abstract class BaseController { protected logger : Logger ; constructor ( logger ) { this . logger = logger ; } } interface HomeView { messages ? : any [ ] ; errors ? : any [ ] ; account ? : any ; } interface SettingsView { messages ? : any [ ] ; errors ? : any [ ] ; account ? : any ; addresses : any [ ] ; } export class ApplicationController extends BaseController { private storageManager : StorageManager ; constructor ( logger , storageManager ) { super ( logger . child ( { component : "ApplicationController" } ) ) ; this . storageManager = storageManager ; } home = ( req , res ) => { let view = { messages : req . flash ( "message" ) , errors : req . flash ( "error" ) } ; if ( req . user !== undefined ) { view . account = req . user ; } res . render ( "index" , view ) ; } ; completeRegistration = ( req , res ) => { this . storageManager . register ( req . body . name , req . body . email , req . body . password ) . then ( ( account ) => { req . login ( account , function ( err ) { if ( err ) { throw new Error ( err . toString ( ) ) ; } req . flash ( "message" , "Welcome, " + account . name + "!" ) ; return res . redirect ( "/settings" ) ; } ) ; } ) . catch ( ( err ) => { req . flash ( "error" , "Error creating account." ) ; return res . redirect ( "/" ) ; } ) ; } ; settings = ( req , res ) => { req . user . getAddresses ( ) . then ( ( addresses ) => { let view = { messages : req . flash ( "message" ) , errors : req . flash ( "error" ) , account : req . user , addresses : addresses } ; res . render ( "settings" , view ) ; } ) ; } ; addAddress = ( req , res ) => { this . storageManager . addAddress ( req . user , req . body . street , req . body . city , req . body . state , req . body . zip ) . then ( ( ) => { req . flash ( "message" , "Created address!" ) ; return res . redirect ( "/settings" ) ; } ) . catch ( ( err ) => { req . flash ( "error" , "Error creating address." ) ; return res . redirect ( "/settings" ) ; } ) ; } ; }