-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
user store insert #17
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9101075
user store insert
80e46ca
insert user and get user by id
e210044
updated insert and getuserbyid function
35e28c9
functions for insert user , update user, delete user, get user by id
e5356e6
updated variables in functions
7e5641b
changed time in functions created and updated
74b8ac0
last update on time.now()
cfedb9d
changes v_1 on created, updated and time.Time
d23f3d6
Merge branch 'master' of github.com:inplayer-org/backend-interns into…
88c7f05
removed createdAt, updatedAt in functions
afff1da
changed return types in functions
0ea73a2
return values change in user store
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package store |
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 |
---|---|---|
@@ -1 +1,75 @@ | ||
package store | ||
|
||
import ( | ||
"bankacc/pkg/entities" | ||
"database/sql" | ||
"time" | ||
) | ||
|
||
type UserStore interface { | ||
Insert(fullName string, email string, phoneNumber string, createdAt time.Time, updatedAt time.Time) (*entities.User, error) | ||
GetUserById(id int) (*entities.User, error) | ||
UpdateUser(id int, fullName string, email string, phoneNumber string, updatedAt time.Time) (*entities.User, error) | ||
DeleteUser(id int) (*entities.User, error) | ||
} | ||
|
||
type UserModel struct { | ||
Db *sql.DB | ||
} | ||
|
||
func NewUserStoreModel(db *sql.DB) *UserModel { | ||
return &UserModel{ | ||
Db: db, | ||
} | ||
} | ||
|
||
func (store *UserModel) InsertUser(fullName string, email string, phoneNumber string, createdAt time.Time, updatedAt time.Time) (*entities.User, error) { | ||
createdAt = time.Now() | ||
updatedAt = time.Now() | ||
user := entities.User{ | ||
FullName: fullName, | ||
Email: email, | ||
PhoneNumber: phoneNumber, | ||
CreatedAt: createdAt, | ||
UpdatedAt: updatedAt, | ||
} | ||
_, err := store.Db.Exec("INSERT INTO User (full_name, email, phone_number, created_at, updated_at) VALUES(?, ?, ?, ?, ?)", fullName, email, phoneNumber, createdAt, updatedAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &user, nil | ||
} | ||
|
||
func (store *UserModel) GetUserById(id int) (*[]entities.User, error) { | ||
var users []entities.User | ||
result, err := store.Db.Query("SELECT * FROM BankAccount.User WHERE id=?", id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var user entities.User | ||
for result.Next() { | ||
err := result.Scan(&user.Id, &user.FullName, &user.Email, &user.PhoneNumber, &user.UpdatedAt, &user.CreatedAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
users = append(users, user) | ||
} | ||
return &users, nil | ||
} | ||
|
||
func (store *UserModel) UpdateUser(id int, fullName string, email string, phoneNumber string, updatedAt time.Time) (*entities.User, error) { | ||
|
||
_, err := store.Db.Exec("UPDATE BankAccount.User SET full_name = ?, email = ?, phone_number = ?, updated_at =? WHERE id = ?", fullName, email, phoneNumber, updatedAt, id) | ||
FilipNikolovski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return nil, err | ||
} | ||
|
||
func (store *UserModel) DeleteUser(id int) (*entities.User, error) { | ||
_, err := store.Db.Exec("DELETE FROM BankAccount.User WHERE id=?", id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return nil, err | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@F1c1cA you don't need to pass createdAt and updatedAt as arguments, they should be created when you execute the query.