Пример #1
0
func (c *Contact) Add(dtx *apicontext.DataContext) error {
	if strings.TrimSpace(c.FirstName) == "" {
		return errors.New("First name is required")
	}
	if strings.TrimSpace(c.LastName) == "" {
		return errors.New("Last name is required")
	}
	if !email.IsEmail(c.Email) {
		return errors.New("Empty or invalid email address")
	}
	if strings.TrimSpace(c.Type) == "" {
		return errors.New("Type can't be empty")
	}
	if strings.TrimSpace(c.Subject) == "" {
		return errors.New("Subject can't be empty")
	}
	if strings.TrimSpace(c.Message) == "" {
		return errors.New("Message can't be empty")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(addContactStmt)
	if err != nil {
		return err
	}
	defer stmt.Close()

	res, err := stmt.Exec(
		c.FirstName, c.LastName, c.Email, c.Phone, c.Subject, c.Message,
		c.Type, c.Address1, c.Address2, c.City, c.State, c.PostalCode, c.Country, c.Brand.ID)
	if err != nil {
		return err
	}

	if id, err := res.LastInsertId(); err != nil {
		return err
	} else {
		c.ID = int(id)
	}

	return nil
}
Пример #2
0
func (c *Contact) Update() error {
	if c.ID == 0 {
		return errors.New("Invalid Contact ID")
	}
	if strings.TrimSpace(c.FirstName) == "" {
		return errors.New("First name is required")
	}
	if strings.TrimSpace(c.LastName) == "" {
		return errors.New("Last name is required")
	}
	if !email.IsEmail(c.Email) {
		return errors.New("Empty or invalid email address")
	}
	if strings.TrimSpace(c.Type) == "" {
		return errors.New("Type can't be empty")
	}
	if strings.TrimSpace(c.Subject) == "" {
		return errors.New("Subject can't be empty")
	}
	if strings.TrimSpace(c.Message) == "" {
		return errors.New("Message can't be empty")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(updateContactStmt)
	if err != nil {
		return err
	}
	defer stmt.Close()
	_, err = stmt.Exec(
		c.FirstName, c.LastName, c.Email, c.Phone, c.Subject, c.Message, c.Type,
		c.Address1, c.Address2, c.City, c.State, c.PostalCode, c.Country, c.Brand.ID, c.ID)

	return err
}
Пример #3
0
func (cr *ContactReceiver) Update() error {
	if cr.ID == 0 {
		return errors.New("Invalid ContactReceiver ID")
	}
	if !email.IsEmail(cr.Email) {
		return errors.New("Empty or invalid email address.")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(updateContactReceiverStmt)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(cr.FirstName, cr.LastName, cr.Email, cr.ID)

	//update type joins
	if len(cr.ContactTypes) > 0 {
		err = cr.DeleteTypeJoinByReceiver()
		if err != nil {
			return err
		}
		for _, ct := range cr.ContactTypes {
			err = cr.CreateTypeJoin(ct)
			if err != nil {
				return err
			}
		}
	}

	return err
}
Пример #4
0
func (cr *ContactReceiver) Add() error {
	if !email.IsEmail(cr.Email) {
		return errors.New("Empty or invalid email address.")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(addContactReceiverStmt)
	if err != nil {
		return err
	}
	defer stmt.Close()

	res, err := stmt.Exec(cr.FirstName, cr.LastName, cr.Email)
	if err != nil {
		return err
	}

	if id, err := res.LastInsertId(); err != nil {
		return err
	} else {
		cr.ID = int(id)
	}
	//add contact types
	if len(cr.ContactTypes) > 0 {
		for _, ct := range cr.ContactTypes {
			err = cr.CreateTypeJoin(ct)
			if err != nil {
				return err
			}
		}
	}
	return nil
}