database/projects.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package database import ( "fmt" "watchman/schema" ) func (s *service) CreateProject(project schema.Project) error { stmt, err := s.db.Prepare("INSERT INTO Projects (ID, Name) VALUES (?, ?)") if err != nil { return fmt.Errorf("error preparing statement: %v", err) } defer stmt.Close() _, err = stmt.Exec(project.ID, project.Name) if err != nil { return fmt.Errorf("error executing statement: %v", err) } return nil } func (s *service) ListAllProjects() ([]schema.Project, error) { rows, err := s.db.Query("SELECT * FROM Projects") if err != nil { return nil, fmt.Errorf("error querying database: %v", err) } defer rows.Close() var projects []schema.Project for rows.Next() { var project schema.Project err := rows.Scan(&project.ID, &project.Name) if err != nil { return nil, fmt.Errorf("error scanning row: %v", err) } projects = append(projects, project) } return projects, nil } // 8263059922 func (s *service) GetProjectByID(projectID string) (schema.Project, error) { row := s.db.QueryRow("SELECT * FROM Projects WHERE ID = ?", projectID) var project schema.Project err := row.Scan(&project.ID, &project.Name) if err != nil { return schema.Project{}, fmt.Errorf("error scanning row: %v", err) } return project, nil } func (s *service) UpdateProjectByID(projectID string, project schema.Project) error { stmt, err := s.db.Prepare("UPDATE Projects SET Name = ?, ID = ? WHERE ID = ?") if err != nil { return fmt.Errorf("error preparing statement: %v", err) } defer stmt.Close() _, err = stmt.Exec(project.Name, project.ID, projectID) if err != nil { return fmt.Errorf("error executing statement: %v", err) } return nil } func (s *service) DeleteProjectByID(projectID string) error { stmt, err := s.db.Prepare("DELETE FROM Projects WHERE ID = ?") if err != nil { return fmt.Errorf("error preparing statement: %v", err) } defer stmt.Close() _, err = stmt.Exec(projectID) if err != nil { return fmt.Errorf("error executing statement: %v", err) } return nil } |