Skip to content

Commit

Permalink
Storepage use
Browse files Browse the repository at this point in the history
  • Loading branch information
alnck committed Feb 15, 2022
1 parent aaff50a commit 04224d1
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func initTemplatesRouteMap(route *gin.Engine) {

route.GET(LINK_TEMPLATE_LOGINPAGE_RELATIVEPATH, templates.LoginPage)
route.GET(LINK_TEMPLATE_REGISTERPAGE_RELATIVEPATH, templates.RegisterPage)
route.GET("/storypage/:id", templates.StoryPage)
}

func initBlogRouteMap(route *gin.RouterGroup, validateTokenHandle, authHandle gin.HandlerFunc) {
Expand Down
3 changes: 2 additions & 1 deletion src/constants/link_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package constants
const (
SERVER_PORT = ":5000"

LINK_MONGODB_URI = "mongodb://localhost:27017"
LINK_MONGODB_URI = "mongodb://localhost:27017"
LINK_MONGODB_DOCKER_URI = "mongodb://blogdb:27017"

LINK_RELATIVEPATH_CONSTANT = "/"

Expand Down
23 changes: 23 additions & 0 deletions src/handler/loginHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package handler

import (
. "blog-on-containers/constants"
"blog-on-containers/entities"
"blog-on-containers/models"
"blog-on-containers/services"
"blog-on-containers/token"
"blog-on-containers/utils"
"net/http"
"time"

Expand Down Expand Up @@ -65,5 +67,26 @@ func genrateJWTToken(context *gin.Context, loginObj models.LoginRequest, userSer

//utils.SetCookie(context, token)

initSampleData(context)

ok(context, http.StatusOK, MESSAGE_TOKEN_GENERATION, token)
}

func initSampleData(context *gin.Context) {
userService := services.NewUserService()

hasData := userService.IsValidUsernameAndPassword(models.LoginRequest{UserName: "admin", Password: "admin"})
if hasData {
return
}

err := userService.CreateUser(models.LoginRequest{UserName: "admin", Password: "admin"})
if err != nil {
return
}

storyService := services.NewStoryService(context)

cu := utils.GetCurrentUser(context)
storyService.CreateStory(entities.NewStory("Golang - Fabrika Tasarım Deseni", "Desing Patternlar, yazılım sürecinde sıkça karşılaşılan sorunların ortak çözüm yoludur. Buradaki sorunlar, runtime sırasında alınan hatalar değildir. Peki nedir? Hiç kodunuzu incelediğinizde “burada bir şey eksik ama” dediğiniz oluyor mu? İşte tam orada devreye Design Patternlar giriyor. Aynı kodun çok defa tekrar etmesi, connect işlemlerinde kendini tekrar eden yapılar v.b. Bu gibi tasarımsal sorunların çözümü Design Patternlardır. Kısacası geçmişten günümüzü deneme yanılma yoluyla ortaya çıkan, ortak sorunların çözümü için oluşturulmuş kalıplardır.", cu.ID))
}
2 changes: 1 addition & 1 deletion src/repository/mongoDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func initMongoDBInstance() {
lock.Lock()
defer lock.Unlock()
if mongoDBInstance == nil {
clientOptions := options.Client().ApplyURI(LINK_MONGODB_URI) //mongodb://blogdb:27017
clientOptions := options.Client().ApplyURI(LINK_MONGODB_DOCKER_URI) //mongodb://blogdb:27017
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal("⛒ Connection Failed to Database")
Expand Down
9 changes: 5 additions & 4 deletions src/services/story-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewStoryServiceForTest(mongoRepo repository.IMongoRepository, context *gin.

func (*StoryService) CreateStory(story entities.Story) bool {
_, err := repoStories.InsertOne(m_COLLECTION_NAME_STORIES, story)
return err != nil
return err == nil
}

func (*StoryService) UpdateStory(story models.StoryRequest) bool {
Expand Down Expand Up @@ -81,7 +81,7 @@ func (*StoryService) DeleteStory() bool {

_, err = repoStories.DeleteOne(m_COLLECTION_NAME_STORIES, filter)

return err != nil
return err == nil

}

Expand All @@ -108,8 +108,9 @@ func (*StoryService) GetStory() (entities.Story, bool) {

filter := bson.M{"_id": id}

if repoStories.Find(m_COLLECTION_NAME_STORIES, filter, &story) != nil {
return story, false
err = repoStories.FindOne(m_COLLECTION_NAME_STORIES, filter, &story)
if err != nil {
return entities.Story{}, false
}

return story, true
Expand Down
125 changes: 125 additions & 0 deletions src/static/css/blog.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* stylelint-disable selector-list-comma-newline-after */

.blog-header {
line-height: 1;
border-bottom: 1px solid #e5e5e5;
}

.blog-header-logo {
font-family: "Playfair Display", Georgia, "Times New Roman", serif;
font-size: 2.25rem;
}

.blog-header-logo:hover {
text-decoration: none;
}

h1, h2, h3, h4, h5, h6 {
font-family: "Playfair Display", Georgia, "Times New Roman", serif;
}

.display-4 {
font-size: 2.5rem;
}
@media (min-width: 768px) {
.display-4 {
font-size: 3rem;
}
}

.nav-scroller {
position: relative;
z-index: 2;
height: 2.75rem;
overflow-y: hidden;
}

.nav-scroller .nav {
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
padding-bottom: 1rem;
margin-top: -1px;
overflow-x: auto;
text-align: center;
white-space: nowrap;
-webkit-overflow-scrolling: touch;
}

.nav-scroller .nav-link {
padding-top: .75rem;
padding-bottom: .75rem;
font-size: .875rem;
}

.card-img-right {
height: 100%;
border-radius: 0 3px 3px 0;
}

.flex-auto {
-ms-flex: 0 0 auto;
flex: 0 0 auto;
}

.h-250 { height: 250px; }
@media (min-width: 768px) {
.h-md-250 { height: 250px; }
}

/*
* Blog name and description
*/
.blog-title {
margin-bottom: 0;
font-size: 2rem;
font-weight: 400;
}
.blog-description {
font-size: 1.1rem;
color: #999;
}

@media (min-width: 40em) {
.blog-title {
font-size: 3.5rem;
}
}

/* Pagination */
.blog-pagination {
margin-bottom: 4rem;
}
.blog-pagination > .btn {
border-radius: 2rem;
}

/*
* Blog posts
*/
.blog-post {
margin-bottom: 4rem;
}
.blog-post-title {
margin-bottom: .25rem;
font-size: 2.5rem;
}
.blog-post-meta {
margin-bottom: 1.25rem;
color: #999;
}

/*
* Footer
*/
.blog-footer {
padding: 2.5rem 0;
color: #999;
text-align: center;
background-color: #f9f9f9;
border-top: .05rem solid #e5e5e5;
}
.blog-footer p:last-child {
margin-bottom: 0;
}
32 changes: 32 additions & 0 deletions src/templates/template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package templates

import (
"blog-on-containers/entities"
"blog-on-containers/services"
"fmt"
"net/http"
"time"

"github.com/gin-gonic/gin"
)
Expand All @@ -13,3 +17,31 @@ func LoginPage(context *gin.Context) {
func RegisterPage(context *gin.Context) {
context.HTML(http.StatusOK, "register.html", nil)
}

type storyVo struct {
CreatedDate time.Time
Title string
Content string
Username string
}

func StoryPage(context *gin.Context) {
var story entities.Story
storyService := services.NewStoryService(context)
story, bool := storyService.GetStory()
if !bool {
return
}

//t, _ := template.ParseFiles("story.html")
//t.Execute(context.Writer, story)

storyVo := storyVo{
CreatedDate: story.CreatedDate,
Title: story.Title,
Content: story.Content,
Username: "username",
}
context.HTML(http.StatusOK, "story.html", storyVo)
fmt.Println(story)
}
24 changes: 24 additions & 0 deletions src/templates/views/story.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Bootstrap CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/blog.css" rel="stylesheet">
</head>
<body>
<main role="main" class="container text-center mt-5">
<div class="row">
<div class="col-md-12 blog-main">
<div class="blog-post">
<h2 class="blog-post-title">{{ .Title }}</h2>
<p class="blog-post-meta">{{.CreatedDate.Format "Jan 02, 2006 15:04 UTC" }} by <a href="#">{{ .Username }}</a></p>
{{ .Content }}
</div><!-- /.blog-post -->

</div><!-- /.blog-main -->
</body>
</html>

0 comments on commit 04224d1

Please sign in to comment.