Inserting Room Restrictions
This commit is contained in:
		@ -11,16 +11,53 @@ func (m *postgresDBRepo) AllUsers() bool {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// InsertReservation inserts a reservation into the database
 | 
			
		||||
func (m *postgresDBRepo) InsertReservation(res models.Reservation) error {
 | 
			
		||||
func (m *postgresDBRepo) InsertReservation(res models.Reservation) (int, error) {
 | 
			
		||||
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
 | 
			
		||||
	defer cancel()
 | 
			
		||||
 | 
			
		||||
	var newId int
 | 
			
		||||
	// statement
 | 
			
		||||
	stmt := `insert into reservations (first_name, last_name, email, phone,
 | 
			
		||||
	            start_date, end_date, room_id, created_at, updated_at)
 | 
			
		||||
	            values ($1, $2, $3, $4, $5, $6, $7, $8, $9)`
 | 
			
		||||
	            values ($1, $2, $3, $4, $5, $6, $7, $8, $9) returning id`
 | 
			
		||||
 | 
			
		||||
	_, err := m.DB.ExecContext(ctx, stmt, res.FirstName, res.LastName, res.Email, res.Phone,
 | 
			
		||||
		res.StartDate, res.EndDate, res.RoomID, res.CreatedAt, res.UpdatedAt)
 | 
			
		||||
	row := m.DB.QueryRowContext(ctx, stmt,
 | 
			
		||||
		res.FirstName,
 | 
			
		||||
		res.LastName,
 | 
			
		||||
		res.Email,
 | 
			
		||||
		res.Phone,
 | 
			
		||||
		res.StartDate,
 | 
			
		||||
		res.EndDate,
 | 
			
		||||
		res.RoomID,
 | 
			
		||||
		time.Now(),
 | 
			
		||||
		time.Now())
 | 
			
		||||
 | 
			
		||||
	err := row.Scan(&newId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return newId, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// InsertRoomRestriction inserts a room restriction into the database
 | 
			
		||||
func (m *postgresDBRepo) InsertRoomRestriction(r models.RoomRestriction) error {
 | 
			
		||||
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
 | 
			
		||||
	defer cancel()
 | 
			
		||||
 | 
			
		||||
	stmt := `insert into room_restrictions (
 | 
			
		||||
                start_date, end_date, room_id, reservation_id, restriction_id,
 | 
			
		||||
                created_at, updated_at)
 | 
			
		||||
	            values ($1, $2, $3, $4, $5, $6, $7)`
 | 
			
		||||
 | 
			
		||||
	_, err := m.DB.ExecContext(ctx, stmt,
 | 
			
		||||
		r.StartDate,
 | 
			
		||||
		r.EndDate,
 | 
			
		||||
		r.RoomID,
 | 
			
		||||
		r.ReservationID,
 | 
			
		||||
		r.RestrictionID,
 | 
			
		||||
		time.Now(),
 | 
			
		||||
		time.Now())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -5,5 +5,6 @@ import "go-udemy-web-1/internal/models"
 | 
			
		||||
type DatabaseRepo interface {
 | 
			
		||||
	AllUsers() bool
 | 
			
		||||
 | 
			
		||||
	InsertReservation(res models.Reservation) error
 | 
			
		||||
	InsertReservation(res models.Reservation) (int, error)
 | 
			
		||||
	InsertRoomRestriction(res models.RoomRestriction) error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user