func Back(givenName string, projectName string, card string, amount string, db *mgo.Database) error { if !models.ValidateName(givenName) { return errors.New("Given name should be no shorter than 4 chars and no longer than 20") } cardArr := models.ParseCard(card) if !models.LuhnCheck(cardArr) { return errors.New("Looks like this card is invalid") } backAmount, err := strconv.ParseFloat(amount, 64) if backAmount < 0 { return errors.New("you entered a negative amount!") } if err != nil { return err } result, err := models.FindProjectByName(projectName, db) if err != nil { return err } if result.HasCard(cardArr) { return errors.New("Looks like this card is already being used") } result.UpdateCard(cardArr, backAmount) if err != nil { return err } User, err := models.FindUserByName(givenName, db) if err != nil { return err } err = User.AddBacking(result.Id, backAmount) if err != nil { return err } err = User.Save(db) if err != nil { return err } err = result.Save(db) if err != nil { return err } return nil }
// check to see if backing a project works or not // we should check the databases with queries to see if the appropriate stuff is created or not func TestBacking(t *testing.T) { assert := assert.New(t) sess, db := utils.MakeDB("minikick") defer sess.Close() proj, err := models.NewProject("testproject", "123.45") //set up a new project err = proj.Save(db) assert.Nil(err) card := "4581237932741116" amount := "123.32" err = Back("faiq", "testproject", card, amount, db) //lets back this project assert.Nil(err) // Now lets make some queries to make sure that everything checks out by querying the tables project, err := models.FindProjectByName("testproject", db) assert.Nil(err) assert.NotNil(project) //make sure its there assert.Contains(project.Cards, models.ParseCard(card)) // check to see if we saved the card parsedAmount, _ := strconv.ParseFloat(amount, 64) assert.Equal(project.AmountBacked, parsedAmount) //check to see if we backed it for that much cash db.DropDatabase() // drop the database to work on the next test }
func List(name string, db *mgo.Database) error { project, err := models.FindProjectByName(name, db) if err != nil { return err } users, err := models.FindUsersForProject(project.Id, db) if err != nil { return err } for _, user := range users { fmt.Printf("%s backed %s for %f\n", user.Name, name, user.BackedProjects[user.BackIndex(project.Id)].Amount) } remaining := project.TargetAmount - project.AmountBacked if remaining < 0 { // we went over! fmt.Println("%s is successful!", project.Name) } else { fmt.Println("%s needs %f more dollars to be successful!", project.Name, remaining) } return nil }