From 0536f479868d2de0a251351393d380f42214c16b Mon Sep 17 00:00:00 2001 From: "Jorge E. Astorga" Date: Mon, 15 May 2017 21:44:30 -0700 Subject: [PATCH] saved the latest code for orm-contacts - NOTE: code not working properly --- postgres/client.go | 1 + postgres/contact_service.go | 54 +++++++++++++++++++++++++++++++++++++ postgres/session.go | 1 + 3 files changed, 56 insertions(+) create mode 100644 postgres/client.go create mode 100644 postgres/contact_service.go create mode 100644 postgres/session.go diff --git a/postgres/client.go b/postgres/client.go new file mode 100644 index 0000000..bf560be --- /dev/null +++ b/postgres/client.go @@ -0,0 +1 @@ +package postgres diff --git a/postgres/contact_service.go b/postgres/contact_service.go new file mode 100644 index 0000000..2691790 --- /dev/null +++ b/postgres/contact_service.go @@ -0,0 +1,54 @@ +package postgres + +import ( + "github.com/jorgeastorga/contakx/contakx" + _"database/sql" + "github.com/jinzhu/gorm" + "log" +) + +type ContactService struct { + AppDB *gorm.DB +} + +///ContactService returns a user for a given id. +func (c *ContactService) Contact(id int) (contact contakx.Contact, err error){ + //var *contact contakx.Contact + c.AppDB.First(&contact, id) + return contact, err +} + +func (c *ContactService) CreateContact(contact *contakx.Contact) error { + err := c.AppDB.Create(&contakx.Contact{ + FirstName:contact.FirstName, + LastName: contact.LastName, + Address1: contact.Address1, + Address2: contact.Address2, + City: contact.City, + State: contact.State, + ZipCode: contact.ZipCode, + Company: contact.Company, + Email: contact.Email, + Phone: contact.Phone}).Error + + if err != nil { + log.Println("Error creating the contact: ", err.Error()) + } + + return err +} + +func (c *ContactService)OpenDB(dbConnection string){ + //Database Connection Setup: PostgreSQL + var err error + c.AppDB, err = gorm.Open("postgres", dbConnection) + + if err != nil{ + log.Println("Failed to connect to the database", err.Error()) + } else { + log.Println("DB Connection: connected to the database successfully") + } + +} + + diff --git a/postgres/session.go b/postgres/session.go new file mode 100644 index 0000000..bf560be --- /dev/null +++ b/postgres/session.go @@ -0,0 +1 @@ +package postgres