feat: remake mocks only on posts

This commit is contained in:
Yanislav Igonin 2021-08-31 19:48:02 +03:00
parent 045dcf6fd2
commit 872a6b58df
4 changed files with 22 additions and 37 deletions

View File

@ -26,10 +26,12 @@ func getFile(id, postId int, name string) File {
} }
} }
func getPost(id, threadID int) Post { func getPost(id int) Post {
return Post{ return Post{
ID: id, ID: id,
ThreadID: threadID, IsParent: true,
ParentID: 0,
IsDeleted: false,
Title: randSeq(rand.Intn(100)), Title: randSeq(rand.Intn(100)),
Text: randSeq(rand.Intn(100)), Text: randSeq(rand.Intn(100)),
IsSage: false, IsSage: false,
@ -38,27 +40,16 @@ func getPost(id, threadID int) Post {
getFile(1, id, "https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png"), getFile(1, id, "https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png"),
}, },
CreatedAt: time.Now(), CreatedAt: time.Now(),
}
}
func getThread(id int) Thread {
return Thread{
ID: id,
CreatedAt: time.Now(),
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
Posts: []Post{
getPost(1, id),
getPost(1, id),
},
} }
} }
var ThreadsDb = []Thread{} var PostsDb = []Post{}
func SeedMocks() { func SeedMocks() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
for i := 1; i < 100; i++ { for i := 1; i < 10; i++ {
ThreadsDb = append(ThreadsDb, getThread(i)) PostsDb = append(PostsDb, getPost(i))
} }
} }

View File

@ -2,21 +2,17 @@ package repositories
import "time" import "time"
type Thread struct {
ID int `json:"id"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Posts []Post `json:"posts"`
}
type Post struct { type Post struct {
ID int `json:"id"` ID int `json:"id"`
ThreadID int `json:"-"` IsParent bool `json:"-"`
ParentID int `json:"parentId"`
IsDeleted bool `json:"-"`
Title string `json:"title"` Title string `json:"title"`
Text string `json:"text"` Text string `json:"text"`
IsSage bool `json:"isSage"` IsSage bool `json:"isSage"`
Files []File `json:"files"` Files []File `json:"files"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"-"`
} }
type File struct { type File struct {

View File

@ -4,7 +4,7 @@ type ThreadsRepository struct{}
var Threads ThreadsRepository var Threads ThreadsRepository
func (r *ThreadsRepository) Get(limit, offset int) ([]Thread, error) { func (r *ThreadsRepository) Get(limit, offset int) ([]Post, error) {
// conn, err := Db.Pool.Acquire(context.TODO()) // conn, err := Db.Pool.Acquire(context.TODO())
// if err != nil { // if err != nil {
// return nil, err // return nil, err
@ -76,7 +76,7 @@ func (r *ThreadsRepository) Get(limit, offset int) ([]Thread, error) {
// } // }
// rows.Close() // rows.Close()
return ThreadsDb, nil return PostsDb, nil
// return nil, nil // return nil, nil
} }

View File

@ -46,12 +46,10 @@
<div class="container"> <div class="container">
<div class="row row-cols-auto gy-4"> <div class="row row-cols-auto gy-4">
{{range $Thread := .}} {{range $Post := .}}
<div class="col col-sm-6 col-md-4 col-lg-3"> <div class="col col-sm-6 col-md-4 col-lg-3">
{{$FirstPost := index $Thread.Posts 0}}
<div class="card"> <div class="card">
{{$FirstFile := index $FirstPost.Files 0}} {{$FirstFile := index $Post.Files 0}}
<img <img
src="{{$FirstFile.Name}}" src="{{$FirstFile.Name}}"
@ -59,9 +57,9 @@
alt="Uploaded picture" alt="Uploaded picture"
> >
<div class="card-body"> <div class="card-body">
<h5 class="card-title">{{$FirstPost.Title}}</h5> <h5 class="card-title">{{$Post.Title}}</h5>
<p class="card-text">{{$FirstPost.Text}}</p> <p class="card-text">{{$Post.Text}}</p>
<a href="/{{$Thread.ID}}" class="btn btn-outline-primary">Open</a> <a href="/{{$Post.ID}}" class="btn btn-outline-primary">Open</a>
</div> </div>
</div> </div>
</div> </div>