Skip to content

Commit

Permalink
fix and ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
aasch2020 committed Feb 21, 2024
2 parents 0417bc8 + 361f899 commit 1cfb636
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*.dll
*.so
*.dylib

*.db
# Test binary, built with `go test -c`
*.test

Expand Down
4 changes: 1 addition & 3 deletions crud/db_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ func GetCatId(Cid int) (Category, bool, error) {
counter += 1
log.Println(counter)
rows.Scan(&cat.CatID, &cat.UserID, &cat.Name, &cat.Color)
log.Println("finding")
}
rows.Close()
log.Println("done finding")
log.Println(counter)

return cat, counter == 1, err
}

Expand Down
16 changes: 11 additions & 5 deletions crud/db_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var DB *sqlx.DB
func LoadDumbData() error {
// No recur patterns since we aren't using them yet
for i := 1000; i < 1500; i++ {
task := Task{TaskID: i, UserID: "test_user_id", Category: "yeeet", TaskName: "some name" + strconv.Itoa(i), Description: "sumdesc" + strconv.Itoa(i), StartTime: time.Now(), EndTime: time.Now(), Status: "todo", IsRecurring: false, IsAllDay: false, CronExpression: "dummycron", Difficulty: "easy"}
task := Task{TaskID: i, UserID: "test_user_id", Category: "test_category", TaskName: "some name" + strconv.Itoa(i), Description: "sumdesc" + strconv.Itoa(i), StartTime: time.Now(), EndTime: time.Now(), Status: "todo", IsRecurring: false, IsAllDay: false, CronExpression: "dummycron", Difficulty: "easy"}
lol, _, err := CreateTask(task)
if !lol || (err != nil) {
return err
Expand All @@ -30,6 +30,12 @@ func LoadDumbData() error {
return err2
}
}

bossAdded, err := AddBoss(Boss{BossID: 1, Name: "testboss_name", Health: 30, Image: "../images/clown.jpeg"})
if !bossAdded || err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -100,22 +106,22 @@ func CalculatePoints(difficulty string) int {
}

// for recurrence work in the future
func CreateRecurringLogEntry(taskID int, isCurrent bool, status string) (bool, int64, error) {
func CreateRecurringLogEntry(taskID int, status string, timestamp time.Time) (bool, int64, error) {
tx, err := DB.Beginx()
if err != nil {
fmt.Printf("CreateRecurringLog(): breaky 1: %v\n", err)
return false, -1, err
}
defer tx.Rollback()

stmt, err := tx.Preparex("INSERT INTO RecurringLog (TaskID, isCurrent, Status) VALUES (?, ?, ?)")
stmt, err := tx.Preparex("INSERT INTO RecurringLog (TaskID, Status, timestamp) VALUES (?, ?, ?)")
if err != nil {
fmt.Printf("CreateRecurringLog(): breaky 2: %v\n", err)
return false, -1, err
}
defer stmt.Close()

res, err := stmt.Exec(taskID, isCurrent, status)
res, err := stmt.Exec(taskID, status, timestamp)
if err != nil {
fmt.Printf("CreateRecurringLog(): breaky 3: %v\n", err)
return false, -1, err
Expand Down Expand Up @@ -194,7 +200,7 @@ func PopRecurringTasksMonth() error {
for _, nextTime := range nextTimes {
// Check if the next occurrence is in the current month
if nextTime.Month() == currentMonth {
_, _, err = CreateRecurringLogEntry(task.TaskID, false, "todo")
_, _, err = CreateRecurringLogEntry(task.TaskID, "todo", time.Now())
if err != nil {
fmt.Printf("In here")
return err
Expand Down
87 changes: 50 additions & 37 deletions crud/db_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func GetUserTaskDateTime(uid string, startq time.Time, endq time.Time) ([]RecurT
rows.Close()
return utaskArr, err
}
taskprev.RecurrenceId = "";
taskprev.RecurrenceId = ""
utaskArr = append(utaskArr, taskprev)
}
prep.Close()
Expand Down Expand Up @@ -174,7 +174,7 @@ func CreateTask(task Task) (bool, int64, error) {
return false, -1, err
}
for i := 0; i < 5; i++ {
_, err := rStmnt.Exec(taskID, task.Status,nexTime[i] )
_, err := rStmnt.Exec(taskID, task.Status, nexTime[i])

if err != nil {
// fmt.Println(task)
Expand All @@ -185,8 +185,6 @@ func CreateTask(task Task) (bool, int64, error) {
defer rStmnt.Close()

}



tx.Commit() //commit transaction to database
// fmt.Println("WE ADDED A TASK")
Expand All @@ -202,8 +200,8 @@ func EditTask(task Task, tid int) (bool, error) {

stmt, err := tx.Preparex(`
UPDATE TaskTable
SET UserID = ?, Category = ?, TaskName = ?, Description = ?, StartTime = ?, EndTime = ?, Status = ?, IsRecurring = ?, IsAllDay = ?, Difficulty = ?, CronExpression = ?
WHERE TaskID = ?
SET Category = ?, TaskName = ?, Description = ?, StartTime = ?, EndTime = ?, Status = ?, IsRecurring = ?, IsAllDay = ?, Difficulty = ?, CronExpression = ?
WHERE TaskID = ? AND UserID = ?
`)

if err != nil {
Expand All @@ -213,8 +211,8 @@ func EditTask(task Task, tid int) (bool, error) {

defer stmt.Close()

_, err = stmt.Exec(task.UserID, task.Category, task.TaskName, task.Description, task.StartTime, task.EndTime, task.Status, task.IsRecurring, task.IsAllDay, task.Difficulty, task.CronExpression,
tid)
_, err = stmt.Exec(task.Category, task.TaskName, task.Description, task.StartTime, task.EndTime, task.Status, task.IsRecurring, task.IsAllDay, task.Difficulty, task.CronExpression,
tid, task.UserID)
if err != nil {
log.Printf("EditTask() #3: %v", err)
return false, err
Expand All @@ -229,7 +227,7 @@ func EditTask(task Task, tid int) (bool, error) {
return true, nil
}

func DeleteTask(tid int) (bool, error) {
func DeleteTask(tid int, uid string) (bool, error) {
tx, err := DB.Beginx()
if err != nil {
log.Printf("DeleteTask() #1: %v", err)
Expand All @@ -243,39 +241,47 @@ func DeleteTask(tid int) (bool, error) {
// return false, err
// }

stmt1, err := tx.Preparex("DELETE FROM RecurringLog WHERE TaskID = ?")
delTT, err := tx.Preparex("DELETE FROM TaskTable WHERE TaskID = ? AND UserID = ?")
if err != nil {
log.Printf("DeleteTask() #2: can't preparing statement for RecurringLog deletion: %v", err)
log.Printf("DeleteTask() #2: %v", err)
return false, err
}
defer stmt1.Close()
defer delTT.Close()

_, err = stmt1.Exec(tid)
res, err := delTT.Exec(tid, uid)
if err != nil {
log.Printf("DeleteTask() #3: Error deleting from RecurringLog: %v", err)
log.Printf("DeleteTask() #3: %v", err)
return false, err
}

stmt2, err := tx.Preparex("DELETE FROM TaskTable WHERE TaskID = ?")
numDeleted, err := res.RowsAffected()
if err != nil {
log.Printf("DeleteTask() #4: %v", err)
return false, err
}
defer stmt2.Close()

_, err = stmt2.Exec(tid)
if err != nil {
log.Printf("DeleteTask() #5: %v", err)
return false, err
// Only delete from RecurringLog (not valited by UID) if any were deleted from the main table
if numDeleted > 0 {
delRL, err := tx.Preparex("DELETE FROM RecurringLog WHERE TaskID = ?")
if err != nil {
log.Printf("DeleteTask() #5: can't preparing statement for RecurringLog deletion: %v", err)
return false, err
}
defer delRL.Close()

_, err = delRL.Exec(tid)
if err != nil {
log.Printf("DeleteTask() #6: Error deleting from RecurringLog: %v", err)
return false, err
}
}

tx.Commit()

return true, nil
}

func Passtask(Tid int) (bool, error) {

func Passtask(Tid int, uid string) (bool, error) {
task, ok, err := GetTaskId(Tid)
if err != nil {
fmt.Printf("Passtask(): breaky 2 %v\n", err)
Expand All @@ -287,6 +293,10 @@ func Passtask(Tid int) (bool, error) {
return false, fmt.Errorf("task not found")
}

if task.UserID != uid {
return false, fmt.Errorf("task not owned by this user")
}

if task.IsRecurring {
_, err := DB.Exec(`
UPDATE RecurringLog
Expand Down Expand Up @@ -366,16 +376,20 @@ func Passtask(Tid int) (bool, error) {
return true, nil
}

func Failtask(Tid int) bool {
func Failtask(Tid int, uid string) (bool, error) {
task, ok, err := GetTaskId(Tid)
if err != nil {
fmt.Printf("Failtask(): breaky %v\n", err)
return false
return false, err
}

if !ok {
fmt.Println("Failtask(): Task not found")
return false
return false, fmt.Errorf("task not found")
}

if task.UserID != uid {
return false, fmt.Errorf("task not owned by this user")
}

if task.IsRecurring {
Expand All @@ -392,31 +406,30 @@ func Failtask(Tid int) bool {
} else {
tx, err := DB.Beginx() //start transaction
if err != nil {
return false
return false, err
}
defer tx.Rollback() // Abort transaction if any error occurs

stmt, err := tx.Preparex(`
UPDATE TaskTable
SET Status = ?
WHERE TaskID = ?
UPDATE TaskTable
SET Status = ?
WHERE TaskID = ?
`)
if err != nil {
return false
return false, err
}
swag, erro := stmt.Exec("failed", Tid)
swag, err := stmt.Exec("failed", Tid)
stmt.Close()
if erro != nil {
print(erro.Error())
if err != nil {
print(err.Error())
print("FailtTask(): breaky 1 ")
fmt.Println(erro)
fmt.Println(err)
fmt.Println(swag)
return false
return false, err
}

tx.Commit()
}

return true

return true, nil
}
18 changes: 9 additions & 9 deletions crud/db_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ type TaskPreview struct {
}

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

Expand Down
Loading

0 comments on commit 1cfb636

Please sign in to comment.