Add attendees
This commit is contained in:
parent
3ba0b8dc26
commit
545c74f214
143
attendees.go
Normal file
143
attendees.go
Normal file
@ -0,0 +1,143 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Attendee struct {
|
||||
AttendeeId int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type AttendeeList struct {
|
||||
Attendees *[]*Attendee `json:"attendees"`
|
||||
}
|
||||
|
||||
func (a *Attendee) Write(w http.ResponseWriter) error {
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(a)
|
||||
}
|
||||
|
||||
func (a *Attendee) Read(json_str string) error {
|
||||
dec := json.NewDecoder(strings.NewReader(json_str))
|
||||
return dec.Decode(a)
|
||||
}
|
||||
|
||||
func (al *AttendeeList) Write(w http.ResponseWriter) error {
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(al)
|
||||
}
|
||||
|
||||
type AttendeeExistsError struct{}
|
||||
|
||||
func (aeu AttendeeExistsError) Error() string {
|
||||
return "Attendee exists"
|
||||
}
|
||||
|
||||
func GetAttendees() (*[]*Attendee, error) {
|
||||
var attendees []*Attendee
|
||||
|
||||
_, err := DB.Select(&attendees, "SELECT * from attendees")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &attendees, nil
|
||||
}
|
||||
|
||||
func InsertAttendee(a *Attendee) error {
|
||||
transaction, err := DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
existing, err := transaction.SelectInt("SELECT count(*) from users where Name=?", a.Name)
|
||||
if err != nil {
|
||||
transaction.Rollback()
|
||||
return err
|
||||
}
|
||||
if existing > 0 {
|
||||
transaction.Rollback()
|
||||
return AttendeeExistsError{}
|
||||
}
|
||||
|
||||
err = transaction.Insert(a)
|
||||
if err != nil {
|
||||
transaction.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
err = transaction.Commit()
|
||||
if err != nil {
|
||||
transaction.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func AttendeeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := GetUserFromSession(r)
|
||||
if err != nil {
|
||||
WriteError(w, 1 /*Not Signed In*/)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "POST" {
|
||||
attendee_json := r.PostFormValue("attendee")
|
||||
if attendee_json == "" {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
var attendee Attendee
|
||||
err := attendee.Read(attendee_json)
|
||||
if err != nil {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
attendee.AttendeeId = -1
|
||||
|
||||
err = InsertAttendee(&attendee)
|
||||
if err != nil {
|
||||
if _, ok := err.(AttendeeExistsError); ok {
|
||||
WriteError(w, 5 /*Attendee Exists*/)
|
||||
} else {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(201 /*Created*/)
|
||||
err = attendee.Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
} else if r.Method == "GET" {
|
||||
var al AttendeeList
|
||||
|
||||
attendees, err := GetAttendees()
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
al.Attendees = attendees
|
||||
err = (&al).Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
/* No PUT or DELETE */
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
}
|
1
db.go
1
db.go
@ -18,6 +18,7 @@ func initDB() *gorp.DbMap {
|
||||
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
|
||||
dbmap.AddTableWithName(User{}, "users").SetKeys(true, "UserId")
|
||||
dbmap.AddTableWithName(Session{}, "sessions").SetKeys(true, "SessionId")
|
||||
dbmap.AddTableWithName(Attendee{}, "attendees").SetKeys(true, "AttendeeId")
|
||||
|
||||
err = dbmap.CreateTablesIfNotExists()
|
||||
if err != nil {
|
||||
|
@ -16,6 +16,7 @@ var error_codes = map[int]string{
|
||||
2: "Unauthorized Access",
|
||||
3: "Invalid Request",
|
||||
4: "User Exists",
|
||||
5: "Attendee Exists",
|
||||
// 5: "Connection Failed", //client-side error
|
||||
999: "Internal Error",
|
||||
}
|
||||
|
@ -62,13 +62,10 @@ module.exports = React.createClass({
|
||||
if (this.props.user.isUser())
|
||||
mainContent = (
|
||||
<Tabs defaultActiveKey={1} id='mainNavigationTabs'>
|
||||
<Tab title="Accounts" eventKey={1} >accounts
|
||||
<Tab title="New Entry" eventKey={1} >accounts
|
||||
</Tab>
|
||||
<Tab title="Securities" eventKey={2} >securities
|
||||
<Tab title="Statistics" eventKey={2} >stats will go here
|
||||
</Tab>
|
||||
<Tab title="Scheduled Transactions" eventKey={3} >Scheduled transactions go here...</Tab>
|
||||
<Tab title="Budgets" eventKey={4} >Budgets go here...</Tab>
|
||||
<Tab title="Reports" eventKey={5} >Reports go here...</Tab>
|
||||
</Tabs>);
|
||||
else
|
||||
mainContent = (
|
||||
|
29
js/models.js
29
js/models.js
@ -1,5 +1,3 @@
|
||||
var Big = require('big.js');
|
||||
|
||||
function getJSONObj(json_input) {
|
||||
if (typeof json_input == "string")
|
||||
return $.parseJSON(json_input)
|
||||
@ -76,6 +74,33 @@ Session.prototype.isSession = function() {
|
||||
this.UserId != empty_session.UserId;
|
||||
}
|
||||
|
||||
function Attendee() {
|
||||
this.AttendeeId = -1;
|
||||
this.Name = "";
|
||||
}
|
||||
|
||||
Attendee.prototype.toJSON = function() {
|
||||
var json_obj = {};
|
||||
json_obj.AttendeeId = this.AttendeeId;
|
||||
json_obj.Name = this.Name;
|
||||
return JSON.stringify(json_obj);
|
||||
}
|
||||
|
||||
Attendee.prototype.fromJSON = function(json_input) {
|
||||
var json_obj = getJSONObj(json_input);
|
||||
|
||||
if (json_obj.hasOwnProperty("AttendeeId"))
|
||||
this.AttendeeId = json_obj.AttendeeId;
|
||||
if (json_obj.hasOwnProperty("Name"))
|
||||
this.Name = json_obj.Name;
|
||||
}
|
||||
|
||||
Attendee.prototype.isAttendee = function() {
|
||||
var empty_attendee = new Attendee();
|
||||
return this.AttendeeId != empty_attendee.AttendeeId ||
|
||||
this.Name != empty_attendee.Name;
|
||||
}
|
||||
|
||||
function Error() {
|
||||
this.ErrorId = -1;
|
||||
this.ErrorString = "";
|
||||
|
1
main.go
1
main.go
@ -69,6 +69,7 @@ func main() {
|
||||
servemux.HandleFunc("/static/", staticHandler)
|
||||
servemux.HandleFunc("/session/", SessionHandler)
|
||||
servemux.HandleFunc("/user/", UserHandler)
|
||||
servemux.HandleFunc("/attendee/", AttendeeHandler)
|
||||
|
||||
listener, err := net.Listen("tcp", ":"+strconv.Itoa(port))
|
||||
if err != nil {
|
||||
|
@ -6,7 +6,6 @@
|
||||
"dependencies": {
|
||||
"babel-preset-react": "^6.16.0",
|
||||
"babelify": "^7.3.0",
|
||||
"big.js": "^3.1.3",
|
||||
"browserify": "^13.1.0",
|
||||
"cldr-data": "^29.0.2",
|
||||
"globalize": "^1.1.1",
|
||||
|
Loading…
Reference in New Issue
Block a user