Skip to content

Commit

Permalink
asdf
Browse files Browse the repository at this point in the history
  • Loading branch information
aasch2020 committed Mar 5, 2024
2 parents 4a487f7 + 402bec8 commit 203a745
Show file tree
Hide file tree
Showing 6 changed files with 418 additions and 15 deletions.
44 changes: 44 additions & 0 deletions API_Interaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,50 @@ Certainly! Below is the documentation for the endpoints related to passing and f
- **Response**:
- **Status Code**: 200 OK

### getTeamTask
- **Endpoint**: `/api/v1/getTeamtask/:id`
- **NOTE**: Replace `:id` with the actual TeamID.
- **Description**: get a teams task by id
- **Response**:
- same as other task fetch endpoints, an array of tasks

### addUserTeam
- **Endpoint**: '/api/v1/addUserTeam/:id/:code'
- id is team id, code is user code
- **Description**: add a user to a team
- **Response**:
- 200ok, or error

### getUserTeams
- **Endpoint**: `/api/v1/getUserTeams/:code`
- **Description**: get teams for theuser with the given code
- **Response**:
- array of team objects, 200 code

### getTeamUsers
- **Endpoint**: `/api/v1/getTeamUsers/:id `
- **NOTE**:take in a team ID
- **Description**: sends back user abstraction array, similar to friends function
- **Response**:
- array of users 200 code

### deleteTeamUser
**Endpoint**: '/api/v1/teamUserDelete/:tid/:code'
- id is team id, code is user code
- **Description**: deletes the user from the team
-**Response**:
- 200 ok or error
### deleteTeam
**Endpoint**: '/api/v1/deleteTeam/:tid'
-**NOTE**:team id as id
-**Description**:deletes the team, send a delete req.
-**Response**:200 ok
- **Description**: sends back user abstraction, similar to friends function
### createTeam
**Endpoint**: '/api/v1/createTeam/:name'
-**Description**:create request
-**Response**:
- 200 ok with teamID

## Authentication endpoints

Expand Down
226 changes: 224 additions & 2 deletions crud/db_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,225 @@ func GetUserTask(uid string) ([]Task, error) {
return utaskArr, err
}

func GetTeamTask(tid int) ([]Task, error) {
utaskArr := []Task{}

prep, err := DB.Preparex(`SELECT TaskID, UserID, Category, TaskName, StartTime, EndTime, Status, IsRecurring, IsAllDay, Description, Difficulty FROM TaskTable
WHERE TeamID = ?;`)
if err != nil {
log.Printf("GetTeamTask() #1: %v", err)
return utaskArr, err
}

rows, err := prep.Query(tid)
if err != nil {
log.Printf("GetTeamTask() #2: %v", err)
rows.Close()
prep.Close()
return utaskArr, err
}

for rows.Next() {
var taskprev Task
err := rows.Scan(&taskprev.TaskID, &taskprev.UserID, &taskprev.Category, &taskprev.TaskName, &taskprev.StartTime, &taskprev.EndTime, &taskprev.Status, &taskprev.IsRecurring, &taskprev.IsAllDay, &taskprev.Description, &taskprev.Difficulty)
if err != nil {
log.Printf("GetTeamTask() #3: %v", err)
rows.Close()
}
utaskArr = append(utaskArr, taskprev)
}
prep.Close()
rows.Close()
return utaskArr, err
}

func AddUserToTeam(tid int64, ucode string) bool {
user, found, err := SearchUserCode(ucode, true)
if !found || err != nil {
log.Printf("AddFriend() #1: could not find other user: %v", err)
return false
}
uid, _ := user["UserID"].(string)
prep, err := DB.Preparex("INSERT INTO TeamMembers (TeamID, UserID) VALUES (?,?)")
if err != nil {
log.Printf("bricked in adduser team")
return false
}
_, err = prep.Exec(tid, uid)
if err != nil {
log.Printf("bricked in adduser team")
return false
}
return true

}

func GetUserTeams(uid string) ([]Team, error) {
uteamArr := []Team{}

prep, err := DB.Preparex("SELECT t.TeamID, t.TeamName FROM TeamMembers z, TeamTable t WHERE UserID = ? AND t.TeamID = z.TeamID ")
if err != nil {
log.Println(err)
return uteamArr, err
}
rows, err := prep.Query(uid)
if err != nil {
log.Println(err)
return uteamArr, err
}
if err != nil {
log.Printf("getuserteamissue")
rows.Close()
return uteamArr, err
}

for rows.Next() {
var taskprev Team
err := rows.Scan(&taskprev.TeamID, &taskprev.Name)
if err != nil {
fmt.Println(err)
rows.Close()
return uteamArr, err
}
log.Println("we found a team")
taskprev.Members, _ = GetTeamUsers(taskprev.TeamID)
uteamArr = append(uteamArr, taskprev)
}

return uteamArr, nil

}

func GetTeamUsers(tid int64) ([]map[string]interface{}, error) {
uarr := []string{}
var users []map[string]interface{}
prep, err := DB.Preparex("SELECT u.UserID FROM UserTable u, TeamMembers m WHERE u.UserID = m.UserID AND m.TeamID = ?")
if err != nil {
log.Printf("getuserteamissue", err)
return users, err
}
rows, err := prep.Query(tid)
if err != nil {
log.Printf("getuserteamissue", err)
rows.Close()
return users, err
}

for rows.Next() {
var uid string
log.Println("found a user")
err := rows.Scan(&uid)
if err != nil {
fmt.Println(err)
rows.Close()
return users, err
}
uarr = append(uarr, uid)
}
rows.Close()
for _, fID := range uarr {
fUser, found, err := GetPublicUser(fID)
if !found || err != nil {
log.Printf("Get TEam useres could not retreive users %v", err)
return users, err
}
log.Println("found a user", fUser)
users = append(users, fUser)
}
return users, err

}

func RemoveUserFromTeam(tid int64, ucode string) bool {
user, found, err := SearchUserCode(ucode, true)
if !found || err != nil {
log.Printf("AddFriend() #1: could not find other user: %v", err)
return false
}
uid, _ := user["UserID"].(string)
prep, err := DB.Preparex("DELETE FROM TeamMembers WHERE TeamID = ? AND UserID = ?")
if err != nil {
log.Printf("bricked in del team")
return false
}
_, err = prep.Exec(tid, uid)
if err != nil {
log.Printf("bricked in delteam")
return false
}
return true

}

func DeleteTeam(tid int64) bool {
tx, err := DB.Beginx() //start transaction
defer tx.Rollback()
stmnt, err := tx.Preparex("DELETE FROM TeamMembers WHERE TeamID = ? ")
if err != nil {
log.Printf("bricked in del team")
return false
}
_, err = stmnt.Exec(tid)
if err != nil {
log.Printf("bricked in del team")
return false
}

stmnt2, err := tx.Preparex("DELETE FROM TeamTable WHERE TeamID = ?")
if err != nil {
log.Printf("bricked in del team")
return false
}

_, err = stmnt2.Exec(tid)
if err != nil {
log.Printf("bricked in del team")
return false
}
tx.Commit()
return true
}

func CreateTeam(name string, uid string) (bool, int64) {
tx, err := DB.Beginx() //start transaction
defer tx.Rollback()
stmnt, err := tx.Preparex("INSERT INTO TeamTable (TeamName) VALUES (?)")
if err != nil {
log.Printf("bricked in add team", err)
return false, 0
}
res, err := stmnt.Exec(name)
if err != nil {
log.Printf("bricked in add team", err)
return false, 0
}
teamins, err := res.LastInsertId()
if err != nil {
// fmt.Println(task)
fmt.Println("CreateTeam(): breaky 3 ", err)
return false, 0
}
tx.Commit()
AddUserToTeamUid(teamins, uid)

return true, teamins
}

func AddUserToTeamUid(tid int64, uid string) bool {
prep, err := DB.Preparex("INSERT INTO TeamMembers (TeamID, UserID) VALUES (?,?)")
if err != nil {
log.Printf("bricked in adduser team", err)
return false
}
_, err = prep.Exec(tid, uid)
if err != nil {
log.Printf("bricked in adduser team", err)
return false
}
return true

}

func GetUserTaskDateTime(uid string, startq time.Time, endq time.Time) ([]RecurTypeTask, error) {
utaskArr := []RecurTypeTask{}

Expand All @@ -85,7 +304,8 @@ func GetUserTaskDateTime(uid string, startq time.Time, endq time.Time) ([]RecurT
return utaskArr, err
}
defer prep.Close()

log.Printf("GetUserTaskDateTime() #2: %v", err)
log.Println(startq)
rows, err := prep.Query(uid, startq, endq)
if err != nil {
log.Printf("GetUserTaskDateTime() #2: %v", err)
Expand All @@ -96,6 +316,7 @@ func GetUserTaskDateTime(uid string, startq time.Time, endq time.Time) ([]RecurT
for rows.Next() {
var taskprev RecurTypeTask
err := rows.Scan(&taskprev.TaskID, &taskprev.UserID, &taskprev.Category, &taskprev.TaskName, &taskprev.StartTime, &taskprev.EndTime, &taskprev.Status, &taskprev.IsRecurring, &taskprev.IsAllDay)
log.Println("found not recur")
if err != nil {
log.Printf("GetUserTaskDateTime() #3: %v", err)
rows.Close()
Expand All @@ -106,8 +327,9 @@ func GetUserTaskDateTime(uid string, startq time.Time, endq time.Time) ([]RecurT
}
prep.Close()
rows.Close()
p2, err := DB.Preparex("SELECT c.TaskID, c.UserID, c.Category, c.TaskName, c.StartTime, c.EndTime, c.Status, c.IsRecurring, c.IsAllDay, l.timestamp, l.LogId FROM TaskTable c, RecurringLog l WHERE l.TaskID == c.TaskID AND c.UserID = ? AND l.timestamp > ? AND l.timestamp < ?;")
p2, err := DB.Preparex("SELECT c.TaskID, c.UserID, c.Category, c.TaskName, c.StartTime, c.EndTime, c.Status, c.IsRecurring, c.IsAllDay, l.timestamp, l.LogId FROM TaskTable c, RecurringLog l WHERE l.TaskID = c.TaskID AND c.UserID = ? AND l.timestamp > ? AND l.timestamp < ?;")
if err != nil {
log.Println("found recur")
log.Printf("GetUserTaskDateTime() #4: %v", err)
return utaskArr, err
}
Expand Down
18 changes: 7 additions & 11 deletions crud/db_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ type Category struct {
Color int
}

type Team struct {
TeamID int64
Name string
Members []map[string]interface{}
}

type Task struct {
TaskID int
UserID string
Expand All @@ -24,17 +30,7 @@ type Task struct {
CronExpression string
}

type TaskPreview struct {
TaskID int
UserID string
Category string
TaskName string
StartTime time.Time
EndTime time.Time
Status string
IsRecurring bool
IsAllDay bool
}


type RecurTypeTask struct {
TaskID int
Expand Down
Loading

0 comments on commit 203a745

Please sign in to comment.