From 60666c71b666026697f7cfd3e92607e6dc4df67c Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 13 Dec 2015 02:19:19 +0100 Subject: importer can now fetch group of cart diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go index 0fc5ecf..95f61f7 100644 --- a/src/helsinki.at/rhimport/importer.go +++ b/src/helsinki.at/rhimport/importer.go @@ -125,6 +125,21 @@ func (ctx *ImportContext) getPassword(cached bool) (err error) { return } +func (ctx *ImportContext) getGroupOfCart() (err error) { + req := getGroupOfCartRequest{} + req.cart = ctx.Cart + req.response = make(chan getGroupOfCartResult) + ctx.RdDb.getGroupOfCartChan <- req + + res := <-req.response + if res.err != nil { + err = res.err + return + } + ctx.GroupName = res.group + return +} + func (ctx *ImportContext) getShowInfo() (err error) { req := getShowInfoRequest{} req.showid = ctx.ShowId @@ -434,7 +449,7 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) { return } else if ctx.GroupName != "" { res.ResponseCode = 500 - res.ErrorString = "Importing to music pools using the group name is not yet implemented" + res.ErrorString = "Importing to music pools is not yet implemented" // TODO: fetch info from dropboxes (import-params) // - add_cart(ctx, res) [200 -> OK] // - add_cut(ctx, res) [200 -> OK] @@ -443,7 +458,7 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) { res.ResponseCode = 500 res.ErrorString = "Importing to a Cart which might not exist is not yet implemented" // TODO: (everything except Cut, GroupName and ShowId must be in context) - // - ctx.GetGroupOfCart(ctx.Cart) + // - ctx.getGroupOfCart(ctx.Cart) // - remove_cart(ctx, res) [200 || 404 -> OK] // - add_cart(ctx, res) [200 -> OK] // - add_cut(ctx, res) [200 -> OK] @@ -456,7 +471,7 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) { res.Cut = ctx.Cut } else { res.ResponseCode = http.StatusBadRequest - res.ErrorString = "The request doesn't contain enough Information to be processed" + res.ErrorString = "The request doesn't contain enough information to be processed" } rhdl.Printf("ImportResult: %+v\n", res) diff --git a/src/helsinki.at/rhimport/rddb.go b/src/helsinki.at/rhimport/rddb.go index dd1dfec..85a3177 100644 --- a/src/helsinki.at/rhimport/rddb.go +++ b/src/helsinki.at/rhimport/rddb.go @@ -41,6 +41,16 @@ type getPasswordRequest struct { response chan getPasswordResult } +type getGroupOfCartResult struct { + group string + err error +} + +type getGroupOfCartRequest struct { + cart uint + response chan getGroupOfCartResult +} + type getShowInfoResult struct { title string carts map[int]int @@ -53,14 +63,16 @@ type getShowInfoRequest struct { } type RdDb struct { - dbh *sql.DB - password_cache map[string]string - getPasswordChan chan getPasswordRequest - getPasswordStmt *sql.Stmt - getShowInfoChan chan getShowInfoRequest - getShowInfoStmt *sql.Stmt - quit chan bool - done chan bool + dbh *sql.DB + password_cache map[string]string + getPasswordChan chan getPasswordRequest + getPasswordStmt *sql.Stmt + getGroupOfCartChan chan getGroupOfCartRequest + getGroupOfCartStmt *sql.Stmt + getShowInfoChan chan getShowInfoRequest + getShowInfoStmt *sql.Stmt + quit chan bool + done chan bool } func (self *RdDb) init(conf *Config) (err error) { @@ -71,6 +83,9 @@ func (self *RdDb) init(conf *Config) (err error) { if self.getPasswordStmt, err = self.dbh.Prepare("select PASSWORD from USERS where LOGIN_NAME = ?;"); err != nil { return } + if self.getGroupOfCartStmt, err = self.dbh.Prepare("select NAME,DEFAULT_LOW_CART,DEFAULT_HIGH_CART from GROUPS where DEFAULT_LOW_CART <= ? and DEFAULT_HIGH_CART >= ?;"); err != nil { + return + } if self.getShowInfoStmt, err = self.dbh.Prepare("select TITLE,MACROS from CART where NUMBER = ?;"); err != nil { return } @@ -78,7 +93,6 @@ func (self *RdDb) init(conf *Config) (err error) { } func (self *RdDb) getPassword(username string, cached bool) (pwd string, err error) { - if cached { pwd = self.password_cache[username] } @@ -97,6 +111,36 @@ func (self *RdDb) getPassword(username string, cached bool) (pwd string, err err return } +func (self *RdDb) getGroupOfCart(cart uint) (group string, err error) { + var rows *sql.Rows + if rows, err = self.getGroupOfCartStmt.Query(cart, cart); err != nil { + return + } + defer rows.Close() + size_min := ^uint(0) + for rows.Next() { + var name string + var low_cart, high_cart uint + if err = rows.Scan(&name, &low_cart, &high_cart); err != nil { + return + } + if high_cart >= low_cart { + size := (high_cart - low_cart) + 1 + if size_min > size { + group = name + size_min = size + } + } + } + if err = rows.Err(); err != nil { + return + } + if group == "" { + err = fmt.Errorf("cart is outside of all group cart ranges") + } + return +} + func (self *RdDb) getShowInfo(showid int) (title string, carts map[int]int, err error) { var macros string err = self.getShowInfoStmt.QueryRow(showid).Scan(&title, ¯os) @@ -127,6 +171,10 @@ func (self *RdDb) dispatchRequests() { } pwd, err := self.getPassword(req.username, req.cached) req.response <- getPasswordResult{pwd, err} + case req := <-self.getGroupOfCartChan: + rhdl.Println("RdDb: got getGroupOfCart request for", req.cart) + group, err := self.getGroupOfCart(req.cart) + req.response <- getGroupOfCartResult{group, err} case req := <-self.getShowInfoChan: rhdl.Println("RdDb: got getShowInfo request for", req.showid) title, carts, err := self.getShowInfo(req.showid) @@ -147,6 +195,9 @@ func (self *RdDb) Cleanup() { if self.getPasswordStmt != nil { self.getPasswordStmt.Close() } + if self.getGroupOfCartStmt != nil { + self.getGroupOfCartStmt.Close() + } if self.getShowInfoStmt != nil { self.getPasswordStmt.Close() } @@ -159,6 +210,7 @@ func NewRdDb(conf *Config) (db *RdDb, err error) { db.done = make(chan bool) db.password_cache = make(map[string]string) db.getPasswordChan = make(chan getPasswordRequest) + db.getGroupOfCartChan = make(chan getGroupOfCartRequest) db.getShowInfoChan = make(chan getShowInfoRequest) if err = db.init(conf); err != nil { -- cgit v0.10.2