Skip to content

Commit

Permalink
'301118'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilnar authored and Ilnar committed Nov 30, 2018
2 parents 84bb4da + 4be3171 commit 347b320
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 35 deletions.
43 changes: 40 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,63 @@
package main

import (
"net/http"
"html/template"
"fmt"

"github.com/tst_martini/models"
"github.com/go-martini/martini"
)

var discounts map[string]*models.discounts
var discounts map[string]*models.Discount

func main() {
discounts := make(map[string]*models.discounts,0)
discounts = make(map[string]*models.Discount,0)
m := martini.Classic()

staticOptions := martini.StaticOptions{Prefix:"static"}
m.Use(martini.Static("static", staticOptions))
m.Get("/", indexHandler)
m.Get("/write", writeHandler)
m.Get("/Save", saveDiscountsHandler)
m.Get("/test", func()string{
return "That's ok!"
})

m.Run()
}

func indexHandler( ){
func indexHandler(w http.ResponseWriter, r *http.Request){
t, err := template.ParseFiles("templates/index.html", "templates/header.html", "templates/footer.html")
if err != nil{
fmt.Fprintf(w, err.Error())
}
t.ExecuteTemplate(w, "index", nil)
}

func writeHandler(w http.ResponseWriter, r *http.Request){
t, err := template.ParseFiles("templates/write.html", "templates/header.html", "templates/footer.html")
if err != nil{
fmt.Fprintf(w, err.Error())
}
t.ExecuteTemplate(w, "write", nil)
}

func saveDiscountsHandler(w http.ResponseWriter, r *http.Request){
id := r.FormValue("id")
title := r.FormValue("title")
description := r.FormValue("description")

var discount *models.Discount
if id != "" {
discount = discounts[id]
discount.Title = title
discount.Description = description
} else {
id = GenerateId()
discount := models.NewDiscount(id, title, description)
discounts[discount.Id] = discount
}

http.Redirect(w, r, "/", 302)
}
19 changes: 12 additions & 7 deletions models/discounts.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package models

type Discounts struct{
id string
description string
is_sc string
id_car string
type Discount struct{
Id string
Title string
Description string
//Id_sc string
//Id_car string
}

func NewDiscounts(id, description, id_sc, id_car string)*Discounts{
return &Discounts{id, description, id_sc, id_car}
//func NewDiscount(id, title, description, id_sc, id_car string)*Discount{
// return &Discount{id, title, description, id_sc, id_car}
//}

func NewDiscount(id, title, description string)*Discount{
return &Discount{id, title, description}
}
7 changes: 7 additions & 0 deletions templates/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ define "footer" }}
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="static/js/bootstrap.min.js"></script>
</body>
</html>

{{ end }}
49 changes: 49 additions & 0 deletions templates/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{{ define "header" }}

<!--<!DOCTYPE html>-->
<html lang="en">
<head>
<h1>Hello world!</h1>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Go Blog Example</title>

<!-- Bootstrap -->
<link href="static/css/bootstrap.min.css" rel="stylesheet">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<link href="static/css/app.css" rel="stylesheet">

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="static/js/jquery-2.1.0.min.js"></script>
</head>
<body>

<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Go Blog Example</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="/write">New Post</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>

{{ end }}
52 changes: 27 additions & 25 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<div>
{{ range $value := .Posts }}
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-8">
{{if $.IsAuthorized}}
<h1><a href="/edit/{{$value.Id}}">{{ $value.Title }}</a></h1>
{{else}}
<h1><a href="/view/{{$value.Id}}">{{ $value.Title }}</a></h1>
{{end}}
</div>
<div class="col-xs-2">
</div>
</div>
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-8">
{{ $value.ContentHtml | unescape }}
</div>
<div class="col-xs-2">
</div>
</div>
{{ end }}
{{ define "index" }}

{{ template "header" }}

{{ range $key, $value := . }}
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-8">
<h1><a href="/edit?id={{$key}}">{{ $value.description }}</a></h1>
</div>
<div class="col-xs-2">
</div>
</div>
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-8">
<!--{{ $value.Content }}-->
</div>
<div class="col-xs-2">
</div>
</div>
{{ end }}

{{ template "footer" }}

{{ end }}
32 changes: 32 additions & 0 deletions templates/write.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{ define "write" }}

{{ template "header" }}

<div class="row">
<div class="col-xs-4">
</div>
<div class="col-xs-4">
<form role="form" method="POST" action="/Save">
<input type="hidden" name="id" value="{{.Id}}" />
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" id="title" name="title" value="{{.Title}}" />
</div>
<div class="form-group">
<label>Description</label>
<textarea id="content" name="content">{{.Description}}</textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

</div>
<div class="col-xs-4">
{{ if .Id }}
<a href="/delete?id={{.Id}}">Delete</a>
{{ end }}
</div>
</div>

{{ template "footer" }}

{{ end }}
23 changes: 23 additions & 0 deletions test/tst_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
)

type Errfunc float64

func (a Errfunc) Error() string {
return "Ошибка елки-палки"
}

func Div(a,b Errfunc ) string {
if b == 0 {
return b.Error()
} else {
return fmt.Sprintf("%d", a/b)
}
}

func main() {
fmt.Print(Div(1, 5))
}
19 changes: 19 additions & 0 deletions test/tst_func_closed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import "fmt"

func fibonacci() func(int) int {
return func(i int) int{
if i > 1{
return (fibonacci()(i-1) + fibonacci()(i-2))
}
return i
}
}

func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f(i))
}
}
20 changes: 20 additions & 0 deletions test/tst_interf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import(
"fmt"
)

type IPAddr [4]byte

func (ip IPAddr) String()string{
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}

func main() {
var a IPAddr
a[0] = 4
a[1] = 5
a[2] = 6
a[3] = 7
fmt.Println(a)
}
22 changes: 22 additions & 0 deletions test/tst_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"strings"
"fmt"
)

func WordCount(s string) {
var str []string
var m map[string]int
m = make(map[string]int)
str = strings.Fields(s)
fmt.Print(str)
for i := range str{
m[str[i]] = m[str[i]] + 1
}
fmt.Print(m)
}

func main() {
WordCount("Are you used to used ?")
}
32 changes: 32 additions & 0 deletions test/tst_map2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"strings"
"fmt"
)

func WordCount(s string) map[string]int {
var str []string

str = splitString(s)
return calcCountMap(str)
}

func splitString(s string) []string{
return strings.Fields(s)
}

func calcCountMap(s []string) map[string]int {
var m map[string]int

m = make(map[string]int)

for i := range s{
m[s[i]] = m[s[i]] + 1
}
return m
}

func main() {
fmt.Print(WordCount("I ate a donut. Then I ate another donut."))
}
12 changes: 12 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"crypto/rand"
"fmt"
)

func GenerateId() string {
b := make([]byte, 16)
rand.Read(b)
return fmt.Sprintf("%x", b)
}

0 comments on commit 347b320

Please sign in to comment.