repository.*BookingPostgres.Create   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 9.95
c 0
b 0
f 0
1
package repository
2
3
import (
4
	"fmt"
5
6
	"github.com/architectv/estate-task/pkg/model"
7
	"github.com/jmoiron/sqlx"
8
)
9
10
type BookingPostgres struct {
11
	db *sqlx.DB
12
}
13
14
func NewBookingPostgres(db *sqlx.DB) *BookingPostgres {
15 1
	return &BookingPostgres{db: db}
16
}
17
18
func (r *BookingPostgres) Create(booking *model.Booking) (int, error) {
19 1
	var id int
20 1
	query := fmt.Sprintf(
21
		`INSERT INTO %s (room_id, date_start, date_end) VALUES ($1, $2, $3) RETURNING id`,
22
		bookingsTable)
23 1
	row := r.db.QueryRow(query, booking.RoomId, booking.DateStart, booking.DateEnd)
24 1
	if err := row.Scan(&id); err != nil {
25 1
		return 0, err
26
	}
27
28 1
	return id, nil
29
}
30
31
func (r *BookingPostgres) Delete(id int) error {
32 1
	query := fmt.Sprintf("DELETE FROM %s WHERE id=$1", bookingsTable)
33 1
	_, err := r.db.Exec(query, id)
34
35 1
	return err
36
}
37
38
func (r *BookingPostgres) GetByRoomId(roomId int) ([]*model.Booking, error) {
39 1
	var bookings []*model.Booking
40
41 1
	query := fmt.Sprintf(
42
		`SELECT * FROM %s WHERE room_id=$1 ORDER BY date_start`, bookingsTable)
43 1
	err := r.db.Select(&bookings, query, roomId)
44
45 1
	return bookings, err
46
}
47
48
func (r *BookingPostgres) GetById(id int) (*model.Booking, error) {
49 1
	booking := &model.Booking{}
50 1
	query := fmt.Sprintf("SELECT * FROM %s WHERE id=$1", bookingsTable)
51 1
	err := r.db.Get(booking, query, id)
52
53 1
	return booking, err
54
}
55