pkg/repository/booking_postgres.go   A
last analyzed

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 6
eloc 33
dl 0
loc 53
ccs 18
cts 18
cp 1
crap 6
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A repository.*BookingPostgres.GetByRoomId 0 8 1
A repository.*BookingPostgres.Create 0 11 2
A repository.*BookingPostgres.Delete 0 5 1
A repository.*BookingPostgres.GetById 0 6 1
A repository.NewBookingPostgres 0 2 1
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