Example #1
0
//UnmarshalBooking TODO: write stuff
func UnmarshalBooking(data []byte, b *bookit.Booking) error {
	var pb Booking
	if err := proto.Unmarshal(data, &pb); err != nil {
		return err
	}

	//Convert back to UTC time
	b.CreateTime = time.Unix(0, pb.CreateTime).UTC()
	b.ModTime = time.Unix(0, pb.ModTime).UTC()
	//Convert back to domain type
	b.ID = bookit.BookingID(pb.ID)
	b.BookingDate = pb.BookingDate
	b.RespContCustomer = pb.RespContCustomer
	b.RespContSeller = pb.RespContSeller
	b.ProjectCode = pb.ProjectCode

	return nil

}
//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()

}