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 all commits
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
60 changes: 60 additions & 0 deletions core/database/tag.go
@@ -0,0 +1,60 @@
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"
)

// AddNewTag adds a tag to the database
func (db *Database) AddNewTag(t *model.Tag, c *model.Club) error {
input := &dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
bobheadxi marked this conversation as resolved.
Show resolved Hide resolved
"pk": {S: aws.String(t.ApplicantID)},
"sk": {S: aws.String(t.PeriodEventID)},
"tag_name": {S: aws.String(t.TagName)},
"type": {S: aws.String("tag")},
},
TableName: aws.String("ClubData-" + c.ID),
}
if _, err := db.c.PutItem(input); err != nil {
return err
}
return nil
}

// GetTag gets the tag associated with Applicant_ID, Period_ID, & Event_ID
func (db *Database) GetTag(applicantID string, periodID string, eventID string, c *model.Club) (*model.Tag, error) {
input := &dynamodb.GetItemInput{
TableName: aws.String("ClubData-" + c.ID),
Key: map[string]*dynamodb.AttributeValue{
"pk": {S: aws.String(applicantID)},
"sk": {S: aws.String(periodID + "_" + eventID)},
},
}
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
}

// DeleteTag associated with Applicant_ID, Period_ID, & Event_ID
func (db *Database) DeleteTag(applicantID string, periodID string, eventID string, c *model.Club) error {
input := &dynamodb.DeleteItemInput{
TableName: aws.String("ClubData-" + c.ID),
Key: map[string]*dynamodb.AttributeValue{
"pk": {S: aws.String(applicantID)},
"sk": {S: aws.String(periodID + "_" + eventID)},
},
}
if _, err := db.c.DeleteItem(input); err != nil {
return err
}
return nil
}
82 changes: 82 additions & 0 deletions core/database/tag_test.go
@@ -0,0 +1,82 @@
package database

import (
"reflect"
"testing"

"github.com/ubclaunchpad/pinpoint/core/model"
)

func TestDatabase_AddNewTag_GetTag(t *testing.T) {
type args struct {
c *model.Club
cu *model.ClubUser
tg *model.Tag
}
type errs struct {
addClub bool
getClub bool
addTag bool
getTag bool
deleteTag bool
}
tests := []struct {
name string
args args
err errs
}{
{"valid", args{
&model.Club{
ID: "1234",
Name: "Launchpad",
Description: "1337 h4x0r",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ˜‰

},
&model.ClubUser{
ClubID: "1234",
Email: "abc@def.com",
UserName: "Bob Ross",
Role: "President",
},
&model.Tag{
ApplicantID: "1234",
PeriodEventID: "1234_1233",
TagName: "Sponsorship Team",
},
}, errs{false, false, false, false, false}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db, _ := NewTestDB()
defer db.DeleteClub(tt.args.c.ID)
if err := db.AddNewClub(tt.args.c, tt.args.cu); (err != nil) != tt.err.addClub {
t.Errorf("Database.AddNewClub() error = %v, wantErr %v", err, tt.err.addClub)
}

club, err := db.GetClub(tt.args.c.ID)
if (err != nil) != tt.err.getClub {
t.Errorf("Database.GetClub() error = %v, wantErr %v", err, tt.err.getClub)
}
if !reflect.DeepEqual(tt.args.c, club) {
t.Errorf("Failed to get expected club, expected: %+v, actual %+v", tt.args.c, club)
return
}

if err := db.AddNewTag(tt.args.tg, tt.args.c); (err != nil) != tt.err.addTag {
t.Errorf("Database.AddNewTag() error = %v, wantErr %v", err, tt.err.addTag)
}

tag, err := db.GetTag(tt.args.tg.ApplicantID, "1234", "1233", tt.args.c)
if (err != nil) != tt.err.getTag {
t.Errorf("Database.GetTag() error = %v, wantErr %v", err, tt.err.getTag)
}
if !reflect.DeepEqual(tt.args.tg, tag) {
t.Errorf("Failed to get expected tag, expected: %+v, actual %+v", tt.args.tg, tag)
return
}

if err := db.DeleteTag(tt.args.tg.ApplicantID, "1234", "1233", tt.args.c); (err != nil) != tt.err.deleteTag {
t.Errorf("Database.DeleteTag() error = %v, wantErr %v", err, tt.err.deleteTag)
}
})
}
}
8 changes: 8 additions & 0 deletions core/model/tag.go
@@ -0,0 +1,8 @@
package model

// Tag model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

succinct, but it works πŸ˜…

type Tag struct {
ApplicantID string `json:"pk"`
PeriodEventID string `json:"sk"`
TagName string `json:"tag_name"`
}