Esempio n. 1
0
//UpdateBooking takes a booking with updated values and replace the one in db. Returns an id to confirm what booking was updated and an error
func (s *BookingService) UpdateBooking(b *bookit.Booking) error {
	// TODO: Create UpdateBooking service function

	tx, err := s.client.db.Begin(true)
	if err != nil {
		//fix error handling
		log.Fatal(err)
	}
	defer tx.Rollback()

	var bk bookit.Booking
	//Get db bucket
	bu := tx.Bucket([]byte("Bookings"))

	//Fetch booking, key is ID
	v := bu.Get([]byte(b.ID))
	if v == nil {
		return bookit.ErrBookingNotFound
	} else if err := internal.UnmarshalBooking(v, &bk); err != nil {
		//check for unmarshalling error
		return bookit.ErrBookingUnmarshal
	}

	b1 := reflect.ValueOf(b)
	b2 := reflect.ValueOf(&bk)

	fmt.Println(b1)
	fmt.Println(b2)

	//Compare if any change was done from what is stored in database
	if b1 == b2 {
		return bookit.ErrBookingNoChange
	}

	//We check if bookings are different above, since bookings were different, we now update Modified time in the new booking
	t := s.client.Now()
	b.ModTime = t

	//After modified time is update, we put new booking back in bucket
	//Marshal protobuf and add to bolt db
	if v, err := internal.MarshalBooking(b); err != nil {
		return err
	} else if err := bu.Put([]byte(b.ID), v); err != nil {
		return err
	}

	return tx.Commit()

}
Esempio n. 2
0
//CreateBooking - creates a new booking and saves to bookings bucket
func (s *BookingService) CreateBooking(b *bookit.Booking) error {

	//Most likely unecessary since we generate Id prior to this method call
	if b.ID == "" {
		return bookit.ErrBookingIDRequired
	}

	//start writing to database
	tx, err := s.client.db.Begin(true)
	if err != nil {
		return err
	}
	defer tx.Rollback()

	//Check that booking do not exist
	//Should not happen since each booking got unique id
	//TODO: Evaluate if’’ below check is needed
	bu := tx.Bucket([]byte("Bookings"))
	if v := bu.Get([]byte(b.ID)); v != nil {
		return bookit.ErrBookingExists
	}

	//Set create and modified time
	t := s.client.Now()
	b.ModTime = t
	b.CreateTime = t

	//Marshal protobuf and add to bolt db
	if v, err := internal.MarshalBooking(b); err != nil {
		return err
	} else if err := bu.Put([]byte(b.ID), v); err != nil {
		return err
	}

	return tx.Commit()

}