-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40adade
commit 5c63dc2
Showing
30 changed files
with
810 additions
and
662 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
SET NAMES utf8 COLLATE 'utf8_unicode_ci'; | ||
SET foreign_key_checks = 1; | ||
SET time_zone = '+00:00'; | ||
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; | ||
SET CHARACTER SET utf8; | ||
|
||
CREATE TABLE IF NOT EXISTS user_status ( | ||
id TINYINT(1) UNSIGNED NOT NULL AUTO_INCREMENT, | ||
|
||
status VARCHAR(25) NOT NULL, | ||
|
||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
deleted TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, | ||
|
||
PRIMARY KEY (id) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS user ( | ||
id VARCHAR(36) NOT NULL, | ||
|
||
first_name VARCHAR(50) NOT NULL, | ||
last_name VARCHAR(50) NOT NULL, | ||
email VARCHAR(100) NOT NULL, | ||
password CHAR(60) NOT NULL, | ||
|
||
status_id TINYINT(1) UNSIGNED NOT NULL DEFAULT 1, | ||
|
||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP DEFAULT 0, | ||
|
||
UNIQUE KEY (email), | ||
CONSTRAINT `f_user_status` FOREIGN KEY (`status_id`) REFERENCES `user_status` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, | ||
|
||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO `user_status` (`id`, `status`, `created_at`, `updated_at`, `deleted`) VALUES | ||
(1, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0), | ||
(2, 'inactive', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package user | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"app/webapi/store" | ||
) | ||
|
||
// Create . | ||
// swagger:route POST /v1/user user UserCreate | ||
// | ||
// Create a user. | ||
// | ||
// Security: | ||
// token: | ||
// | ||
// Responses: | ||
// 201: CreatedResponse | ||
// 400: BadRequestResponse | ||
// 401: UnauthorizedResponse | ||
// 500: InternalServerErrorResponse | ||
func (p *Endpoint) Create(w http.ResponseWriter, r *http.Request) (int, error) { | ||
// swagger:parameters UserCreate | ||
type request struct { | ||
// in: formData | ||
// Required: true | ||
FirstName string `json:"first_name" validate:"required"` | ||
// in: formData | ||
// Required: true | ||
LastName string `json:"last_name" validate:"required"` | ||
// in: formData | ||
// Required: true | ||
Email string `json:"email" validate:"required,email"` | ||
// in: formData | ||
// Required: true | ||
Password string `json:"password" validate:"required"` | ||
} | ||
|
||
// Request validation. | ||
req := new(request) | ||
if err := p.Bind.FormUnmarshal(req, r); err != nil { | ||
return http.StatusBadRequest, err | ||
} else if err = p.Bind.Validate(req); err != nil { | ||
return http.StatusBadRequest, err | ||
} | ||
|
||
// Create the store. | ||
u := store.NewUser(p.DB, p.Q) | ||
|
||
// Check for existing user. | ||
exists, _, err := u.ExistsByField(u, "email", req.Email) | ||
if err != nil { | ||
return http.StatusInternalServerError, err | ||
} else if exists { | ||
return http.StatusBadRequest, errors.New("user already exists") | ||
} | ||
|
||
// Create the user in the database. | ||
ID, err := u.Create(req.FirstName, req.LastName, req.Email, req.Password) | ||
if err != nil { | ||
return http.StatusInternalServerError, err | ||
} | ||
|
||
return p.Response.Created(w, ID) | ||
} |
Oops, something went wrong.