Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

added tag model #79

Merged
merged 6 commits into from Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 56 additions & 1 deletion core/database/database_test.go
Expand Up @@ -3,7 +3,7 @@ package database
import (
"reflect"
"testing"

//"time"
bobheadxi marked this conversation as resolved.
Show resolved Hide resolved
"github.com/ubclaunchpad/pinpoint/core/model"
"github.com/ubclaunchpad/pinpoint/utils"
)
Expand Down Expand Up @@ -107,4 +107,59 @@ func TestClubUser(t *testing.T) {
t.Errorf("Failed to delete user %s", err.Error())
t.FailNow()
}




}

/* Tag Tests */
func TestTag(t *testing.T) {
bobheadxi marked this conversation as resolved.
Show resolved Hide resolved
db := newTestDB(t)
tag := &model.Tag{
Applicant_ID: "1234",
Period_Event_ID: "1234_1233",
Tag_Name: "Sponsorship Team",
}
err := db.AddNewTag(tag)
if err != nil {
t.Errorf("Failed to add new tag: %s", err.Error())
t.FailNow()
}

tagActual, err := db.GetTag(tag.Applicant_ID, "1234" , "1233")
if err != nil {
t.Errorf("Failed to get tag: %s", err.Error())
t.FailNow()
}
if !reflect.DeepEqual(tag, tagActual) {
t.Errorf("tag collected is not as expected, expected: %+v, actual %+v", tag, tagActual)
t.FailNow()
}


tagNew, err := db.ChangeTagName(tag.Applicant_ID, "1234" , "1233", "Marketing Team")
if err != nil {
t.Errorf("Failed to change tag %s", err.Error())
t.Errorf("Failed to change tag %+v", tagNew)
t.FailNow()
}

tag.Tag_Name = "Marketing Team"

tagActual, err = db.GetTag(tag.Applicant_ID, "1234" , "1233")
if err != nil {
t.Errorf("Failed to get tag: %s", err.Error())
t.FailNow()
}
if !reflect.DeepEqual(tagNew, tag) {
t.Errorf("tag collected is not as expected, expected: %+v, actual %+v", tag, tagNew)
t.FailNow()
}

err = db.DeleteTag(tag.Applicant_ID, "1234" , "1233")
if err != nil {
t.Errorf("Failed to delete tag %s", err.Error())
t.FailNow()
}
}
27 changes: 27 additions & 0 deletions core/database/init.go
Expand Up @@ -57,6 +57,33 @@ func (db *Database) initTables() error {
WriteCapacityUnits: aws.Int64(10),
},
},
{
TableName: aws.String("TagTable"),
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("pk"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("sk"),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("pk"),
KeyType: aws.String("HASH"),
},
{
AttributeName: aws.String("sk"),
KeyType: aws.String("RANGE"),
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(10),
WriteCapacityUnits: aws.Int64(10),
},
},
}

// init all tables, collecting critical errors on the way
Expand Down
108 changes: 108 additions & 0 deletions core/database/tag.go
@@ -0,0 +1,108 @@
package database

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/ubclaunchpad/pinpoint/core/model"
)

/* Adds a new tag */
func (db *Database) AddNewTag(t *model.Tag) error {
input := &dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
bobheadxi marked this conversation as resolved.
Show resolved Hide resolved
"pk": {
S: aws.String(t.Applicant_ID),
},
"sk": {
S: aws.String(t.Period_Event_ID),
},
"tag_name": {
S: aws.String(t.Tag_Name),
},
},
TableName: aws.String("TagTable"),
bobheadxi marked this conversation as resolved.
Show resolved Hide resolved
}
if _, err := db.c.PutItem(input); err != nil {
return err
}
return nil
}

/* Gets the tag associated with Applicant_ID, Period_ID, & Event_ID */
func (db *Database) GetTag(Applicant_ID string, Period_ID string, Event_ID string) (*model.Tag, error) {
input := &dynamodb.GetItemInput{
TableName: aws.String("TagTable"),
Key: map[string]*dynamodb.AttributeValue{
"pk": {
S: aws.String(Applicant_ID),
},
"sk": {
S: aws.String(Period_ID + "_" + Event_ID),
},
},
}
result, err := db.c.GetItem(input)
if err != nil {
return nil, err
}
tag := &model.Tag{}
if err := dynamodbattribute.UnmarshalMap(result.Item, tag); err != nil {
return nil, err
}
return tag, nil
}

/* Deletes the tag associated with Applicant_ID, Period_ID, & Event_ID */
func (db *Database) DeleteTag(Applicant_ID string, Period_ID string, Event_ID string) error {
input := &dynamodb.DeleteItemInput{
TableName: aws.String("TagTable"),
Key: map[string]*dynamodb.AttributeValue{
"pk": {
S: aws.String(Applicant_ID),
},
"sk": {
S: aws.String(Period_ID + "_" + Event_ID),
},
},
}
if _, err := db.c.DeleteItem(input); err != nil {
return err
}
return nil
}


/* Changes the tag name associated with Applicant_ID, Period_ID, & Event_ID */
func (db *Database) ChangeTagName(Applicant_ID string, Period_ID string, Event_ID string, New_Name string) (*model.Tag, error) {
input := &dynamodb.UpdateItemInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":new_name": {
S: aws.String(New_Name),
},
},
ExpressionAttributeNames: map[string]*string{
"#attr_name": aws.String("Tag_Name"),
},
TableName: aws.String("TagTable"),
Key: map[string]*dynamodb.AttributeValue{
"pk": {
S: aws.String(Applicant_ID),
},
"sk": {
S: aws.String(Period_ID + "_" + Event_ID),
},
},
UpdateExpression: aws.String("SET #attr_name = :new_name"),
ReturnValues: aws.String("ALL_NEW"),
}
result, err := db.c.UpdateItem(input)
if err != nil {
return nil, err
}
tag := &model.Tag{}
if err := dynamodbattribute.UnmarshalMap(result.Attributes, tag); err != nil {
return nil, err
}
return tag, nil
}
8 changes: 8 additions & 0 deletions core/model/tag.go
@@ -0,0 +1,8 @@
package model

// Club info
type Tag struct {
Applicant_ID string `json:"pk"`
Period_Event_ID string `json:"sk"`
Tag_Name string `json:"tag_name"`
}