@norrchr my intention was to initialize with NewPost and create method which will add will posts
// main.go
package main
import (
"time"
)
type Service struct {
MyPost
MyUser
}
func NewService(u MyUser, p MyPost) *Service {
return &Service{
MyUser: u,
MyPost: p,
}
}
func (s Service) DeleteUser(n int) error {
s.MyUser.DeleteUser(n)
return nil
}
func (s Service) EditUser(n int) {
s.MyUser.EditUser(n)
}
func (s Service) ShowUser(n int) {
s.MyUser.ShowUser(n)
}
func (s Service) CreatePost(n int, p *Post) {
s.MyPost.CreatePost(n, p)
}
func (s Service) DeletePost(n int) {
s.MyPost.DeletePost(n)
}
func (s Service) ShowPost(n int) {
s.MyPost.ShowPost(n)
}
func (s Service) EditPost(n int, p *Post) {
s.MyPost.EditPost(n, p)
}
func main() {
user1 := &User{2, "chad caglak ", "ecaglak", "chad secret"}
post1 := &Post{ID: 1, Content: "yahoo", Created: time.Now()}
post2 := &Post{ID: 2, Content: "google", Created: time.Now()}
post3 := &Post{ID: 3, Content: "bing", Created: time.Now()}
post4 := &Post{ID: 4, Content: "excite", Created: time.Now()}
post5 := &Post{ID: 5, Content: "bing google", Created: time.Now()}
post6 := &Post{ID: 6, Content: "excite altavista", Created: time.Now()}
edit1 := &Post{ID: 7, Content: "edited wow excite altavista", Created: time.Now(), Updated: time.Now()}
edit2 := &Post{ID: 8, Content: "edit tata excite altavista", Created: time.Now(), Updated: time.Now()}
u := NewUser(*user1)
p := NewPost(1, *post1)
s := NewService(u, p)
s.CreatePost(2, post2)
s.CreatePost(3, post3)
s.CreatePost(4, post4)
s.ShowAll()
s.DeletePost(3)
s.ShowAll()
s.ShowPost(2)
s.CreatePost(5, post5)
s.CreatePost(6, post6)
s.ShowAll()
s.EditPost(2, edit1)
s.EditPost(3, edit2)
s.ShowAll()
}
// users.go not complete yet
type MyUser interface {
CreateUser() *User
DeleteUser(int) error
EditUser(int) *User
ShowUser(int)
}
type User struct {
ID int
Name string
Username string
Password string
}
func NewUser(u User) *User {
return &User{
ID: u.ID,
Name: u.Name,
Username: u.Username,
Password: u.Password,
}
}
func (u User) CreateUser() *User {
fmt.Println("user Created")
return &User{
ID: u.ID,
Name: u.Name,
Username: u.Username,
Password: u.Password,
}
}
func (u User) DeleteUser(n int) error {
fmt.Printf("User Delete %v", u.ID)
return nil
}
func (u User) ShowUser(n int) {
fmt.Printf("User Delete %v", u.ID)
}
func (u User) EditUser(n int) *User {
return &User{
ID: u.ID,
Name: u.Name,
Username: u.Username,
Password: u.Password,
}
}
// posts.go
type MyPost interface {
CreatePost(int, *Post)
DeletePost(int)
EditPost(int, *Post)
ShowPost(int)
ShowAll()
}
type Post struct {
ID int
Content string
Created time.Time
Updated time.Time
}
type Posts struct {
post map[int]*Post
}
func NewPost(n int, p Post) *Posts {
return &Posts{
post: map[int]*Post{
n: &Post{
ID: p.ID,
Content: p.Content,
Created: time.Now(),
},
},
}
}
func (p Posts) PNum() int {
return len(p.post) + 1
}
func (p Posts) CreatePost(n int, pp *Post) {
p.post[n] = pp
fmt.Println(" Post Created")
}
func (p Posts) DeletePost(n int) {
delete(p.post, n)
fmt.Println("Delete Post")
}
func (p Posts) EditPost(n int, pp *Post) {
p.post[n] = pp
fmt.Println(" Post Edited")
}
func (p Posts) ShowPost(n int) {
fmt.Printf("Showing Post: %v\n", p.post[n])
}
func (p Posts) ShowAll() {
for _, allp := range p.post {
fmt.Println(allp)
}
}