Datenbanken, erster Versuch
This commit is contained in:
parent
b26544756a
commit
766aedf22d
21 changed files with 678 additions and 320 deletions
67
repositories/course.go
Normal file
67
repositories/course.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
// course
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"sprechstundentool/models"
|
||||
)
|
||||
|
||||
type CourseRepo struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewCourseRepo(db *sql.DB) *CourseRepo {
|
||||
return &CourseRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CourseRepo) FindByName(name string) (models.Course, error) {
|
||||
row := r.db.QueryRow("SELECT * FROM course WHERE name=?", name)
|
||||
var course models.Course
|
||||
if err := row.Scan(&course.Id, &course.Name); err != nil {
|
||||
return models.Course{}, err
|
||||
}
|
||||
return course, nil
|
||||
}
|
||||
|
||||
func (r *CourseRepo) FindById(id int) (models.Course, error) {
|
||||
row := r.db.QueryRow("SELECT * FROM course WHERE id=?", id)
|
||||
var course models.Course
|
||||
if err := row.Scan(&course.Id, &course.Name); err != nil {
|
||||
return models.Course{}, err
|
||||
}
|
||||
return course, nil
|
||||
}
|
||||
|
||||
func (r *CourseRepo) GetAll() ([]models.Course, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM course")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
return r.getFromRows(rows)
|
||||
}
|
||||
|
||||
func (r *CourseRepo) GetActive() ([]models.Course, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM course WHERE active")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
return r.getFromRows(rows)
|
||||
}
|
||||
|
||||
func (r *CourseRepo) getFromRows(rows *sql.Rows) ([]models.Course, error) {
|
||||
var courses []models.Course
|
||||
for rows.Next() {
|
||||
var course models.Course
|
||||
if err := rows.Scan(&course.Id, &course.Name); err != nil {
|
||||
return courses, err
|
||||
}
|
||||
courses = append(courses, course)
|
||||
}
|
||||
return courses, nil
|
||||
}
|
95
repositories/officeHour.go
Normal file
95
repositories/officeHour.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
// officeHour
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"sprechstundentool/models"
|
||||
)
|
||||
|
||||
type OfficeHourRepo struct {
|
||||
db *sql.DB
|
||||
roomRepo *RoomRepo
|
||||
tutorRepo *TutorRepo
|
||||
courseRepo *CourseRepo
|
||||
}
|
||||
|
||||
func NewOfficeHourRepo(db *sql.DB, roomRepo *RoomRepo, tutorRepo *TutorRepo, courseRepo *CourseRepo) *OfficeHourRepo {
|
||||
return &OfficeHourRepo{
|
||||
db: db,
|
||||
roomRepo: roomRepo,
|
||||
tutorRepo: tutorRepo,
|
||||
courseRepo: courseRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) GetAll() ([]models.OfficeHour, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM officeHour")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return r.getFromRows(rows)
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) FindByCourse(course models.Course) ([]models.OfficeHour, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM officeHour WHERE course=?", course.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return r.getFromRows(rows)
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) FindByRoom(room models.Room) ([]models.OfficeHour, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return r.getFromRows(rows)
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) FindById(id int) (models.OfficeHour, error) {
|
||||
return r.getFromRow(r.db.QueryRow("SELECT * FROM officeHour WHERE id=?", id))
|
||||
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
|
||||
var officeHour models.OfficeHour
|
||||
var day, hour, minute, tutorid int
|
||||
var roomName, courseName string
|
||||
err := row.Scan(&officeHour.Id, &tutorid, &day, &hour, &minute, &roomName, &courseName, &officeHour.Week, &officeHour.Info, &officeHour.Active, &officeHour.Duration)
|
||||
if err != nil {
|
||||
return models.OfficeHour{}, err
|
||||
}
|
||||
officeHour.Date = models.Date{day, hour, minute}
|
||||
officeHour.Room, _ = r.roomRepo.FindByName(roomName)
|
||||
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorid)
|
||||
officeHour.Course, _ = r.courseRepo.FindByName(courseName)
|
||||
return officeHour, nil
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) getFromRows(rows *sql.Rows) ([]models.OfficeHour, error) {
|
||||
var officeHours []models.OfficeHour
|
||||
for rows.Next() {
|
||||
var officeHour models.OfficeHour
|
||||
var day, hour, minute, tutorId, roomId, courseId int
|
||||
if err := rows.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &courseId, &officeHour.Week, &officeHour.Info, &officeHour.Active, &officeHour.Duration); err != nil {
|
||||
return officeHours, err
|
||||
}
|
||||
officeHour.Date = models.Date{day, hour, minute}
|
||||
officeHour.Room, _ = r.roomRepo.FindById(roomId)
|
||||
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorId)
|
||||
officeHour.Course, _ = r.courseRepo.FindById(courseId)
|
||||
officeHours = append(officeHours, officeHour)
|
||||
}
|
||||
return officeHours, nil
|
||||
}
|
59
repositories/room.go
Normal file
59
repositories/room.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
// raum
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"sprechstundentool/models"
|
||||
)
|
||||
|
||||
type RoomRepo struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewRoomRepo(db *sql.DB) *RoomRepo {
|
||||
return &RoomRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RoomRepo) FindByName(name string) (models.Room, error) {
|
||||
row := r.db.QueryRow("SELECT * FROM room WHERE name=?", name)
|
||||
var room models.Room
|
||||
if err := row.Scan(&room.Name, &room.MaxOccupy); err != nil {
|
||||
return models.Room{}, err
|
||||
}
|
||||
return room, nil
|
||||
}
|
||||
|
||||
func (r *RoomRepo) FindById(id int) (models.Room, error) {
|
||||
row := r.db.QueryRow("SELECT * FROM room WHERE id=?", id)
|
||||
var room models.Room
|
||||
if err := row.Scan(&room.Id, &room.Name, &room.MaxOccupy); err != nil {
|
||||
return models.Room{}, err
|
||||
}
|
||||
return room, nil
|
||||
}
|
||||
|
||||
func (r *RoomRepo) GetAll() ([]models.Room, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM room")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var rooms []models.Room
|
||||
for rows.Next() {
|
||||
var room models.Room
|
||||
if err := rows.Scan(&room.Id, &room.Name, &room.MaxOccupy); err != nil {
|
||||
return rooms, err
|
||||
}
|
||||
rooms = append(rooms, room)
|
||||
}
|
||||
return rooms, nil
|
||||
}
|
||||
func (r *RoomRepo) Save(room models.Room) error {
|
||||
return nil
|
||||
}
|
||||
func (r *RoomRepo) Add(room models.Room) error {
|
||||
return nil
|
||||
}
|
68
repositories/tutor.go
Normal file
68
repositories/tutor.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
// tutor
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"sprechstundentool/models"
|
||||
)
|
||||
|
||||
type TutorRepo struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewTutorRepo(db *sql.DB) *TutorRepo {
|
||||
return &TutorRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *TutorRepo) FindByEmail(email string) ([]models.Tutor, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM tutor WHERE email=?", email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var tutors []models.Tutor
|
||||
for rows.Next() {
|
||||
var tutor models.Tutor
|
||||
if err := rows.Scan(&tutor.Id, &tutor.Name, &tutor.Email); err != nil {
|
||||
return tutors, err
|
||||
}
|
||||
tutors = append(tutors, tutor)
|
||||
}
|
||||
return tutors, nil
|
||||
}
|
||||
|
||||
func (r *TutorRepo) FindById(id int) (models.Tutor, error) {
|
||||
row := r.db.QueryRow("SELECT * FROM tutor WHERE id=?", id)
|
||||
var tutor models.Tutor
|
||||
if err := row.Scan(&tutor.Id, &tutor.Name, &tutor.Email); err != nil {
|
||||
return models.Tutor{}, err
|
||||
}
|
||||
return tutor, nil
|
||||
}
|
||||
|
||||
func (r *TutorRepo) GetAll() ([]models.Tutor, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM tutor")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var tutors []models.Tutor
|
||||
for rows.Next() {
|
||||
var tutor models.Tutor
|
||||
if err := rows.Scan(&tutor.Id, &tutor.Name, &tutor.Email); err != nil {
|
||||
return tutors, err
|
||||
}
|
||||
tutors = append(tutors, tutor)
|
||||
}
|
||||
return tutors, nil
|
||||
}
|
||||
func (r *TutorRepo) Save(tutor models.Tutor) error {
|
||||
return nil
|
||||
}
|
||||
func (r *TutorRepo) Add(tutor models.Tutor) error {
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue