Skip to content

Commit

Permalink
Merge pull request #67 from Pomog/yurii_dev
Browse files Browse the repository at this point in the history
(m *SqliteBDRepo) CreateThread(thread models.Thread) validation
  • Loading branch information
TartuDen committed Dec 22, 2023
2 parents 03632bd + 7c467ff commit 05ffcd1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
23 changes: 16 additions & 7 deletions internal/handler/homeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,23 @@ func (m *Repository) HomeHandler(w http.ResponseWriter, r *http.Request) {
log.Fatal(err)
}
info.Posts = posts
userWhoCreatedLastPost, err := m.DB.GetUserByID(getUserThatCreatedLastPost(posts))
if err != nil {
setErrorAndRedirect(w, r, "Could not get user as creator, m.DB.GetUserByID(getUserThatCreatedLastPost(posts))", "/error-page")
return
}

info.PictureUserWhoCreatedLastPost = userWhoCreatedLastPost.Picture
info.UserNameWhoCreatedLastPost = userWhoCreatedLastPost.UserName
userIDwhoCreatedLastPost := getUserThatCreatedLastPost(posts)
fmt.Println("getUserThatCreatedLastPost " + strconv.Itoa(userIDwhoCreatedLastPost))
fmt.Println("len(posts) " + strconv.Itoa(len(posts)))

if userIDwhoCreatedLastPost != 0 || len(posts) != 0 {
userWhoCreatedLastPost, err := m.DB.GetUserByID(userIDwhoCreatedLastPost)
if err != nil {
setErrorAndRedirect(w, r, "Could not get user as creator, m.DB.GetUserByID(getUserThatCreatedLastPost(posts))", "/error-page")
return
}

info.PictureUserWhoCreatedLastPost = userWhoCreatedLastPost.Picture
info.UserNameWhoCreatedLastPost = userWhoCreatedLastPost.UserName
} else if userIDwhoCreatedLastPost == 0 || len(posts) == 0 {
info.Created = ""
}
threadsInfo = append(threadsInfo, info)
}

Expand Down
3 changes: 3 additions & 0 deletions internal/renderer/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var app *config.AppConfig

var pathToTemplates = "./template"
var functions = template.FuncMap{
"postsLen": func(allPosts []models.Post) int {
return len(allPosts)
},
"findLastPost": func(allPosts []models.Post) models.Post {
var latestPost models.Post
latestPost.Created, _ = time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05")
Expand Down
4 changes: 4 additions & 0 deletions internal/repository/dbrepo/sqllite.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (m *SqliteBDRepo) CreateThread(thread models.Thread) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

if thread.Subject == "" {
return 0, errors.New("you can not create empty thread")
}

user, err := m.GetUserByID(thread.UserID)
if err != nil {
return 0, errors.New("guest can not create a thread")
Expand Down
6 changes: 5 additions & 1 deletion template/home.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ <h1 class="modal-title fs-5" id="createNewTopicModalLabel">What this topic will
<br><strong>{{ .Subject }} </strong>
</a>
</td>
{{ $lenPosts := postsLen .Posts}}
{{ $lastPost := findLastPost .Posts }}
{{ $dateLastPostCreated := convertTime $lastPost }}
{{ $dateLastPostCreated := "" }}
{{ if gt $lenPosts 0 }}
{{ $dateLastPostCreated = convertTime $lastPost }}
{{ end }}

<td><!-- 2column -->From:<br><img src="{{ .PictureUserWhoCreatedLastPost }}" class="logo_ava" alt="picture"> <br>{{ .UserNameWhoCreatedLastPost }}<br> Posted: {{$dateLastPostCreated}}
{{ $lastPostShorten := shortenPost .Posts}}
Expand Down

0 comments on commit 05ffcd1

Please sign in to comment.