Updated original code

This commit is contained in:
Daniel F 2023-07-11 20:38:26 +01:00
parent 48bf57695b
commit 79f0e0d3d6
3 changed files with 201 additions and 93 deletions

View File

@ -1,46 +1,75 @@
package main package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
"github.com/emilybache/gildedrose-refactoring-kata/gildedrose" "github.com/emilybache/gildedrose-refactoring-kata/lib"
"github.com/emilybache/gildedrose-refactoring-kata/models"
"github.com/emilybache/gildedrose-refactoring-kata/domains"
"github.com/spf13/cobra"
) )
func main() { type TextTestFixtureCommand struct{}
fmt.Println("OMGHAI!")
var items = []*gildedrose.Item{ func (this *TextTestFixtureCommand) Short() string {
{"+5 Dexterity Vest", 10, 20}, return "execute texttest fixture"
{"Aged Brie", 2, 0}, }
{"Elixir of the Mongoose", 5, 7},
{"Sulfuras, Hand of Ragnaros", 0, 80}, func (this *TextTestFixtureCommand) Setup(command *cobra.Command) {}
{"Sulfuras, Hand of Ragnaros", -1, 80},
{"Backstage passes to a TAFKAL80ETC concert", 15, 20}, func (this *TextTestFixtureCommand) Run() lib.CommandRunner {
{"Backstage passes to a TAFKAL80ETC concert", 10, 49}, return func(
{"Backstage passes to a TAFKAL80ETC concert", 5, 49}, env lib.Env,
{"Conjured Mana Cake", 3, 6}, // <-- :O logger lib.Logger,
} itemUpdateServiceProvider domains.ItemUpdateServiceProvider,
) {
days := 2 fmt.Println("OMGHAI!")
var err error
if len(os.Args) > 1 { var itemModels = []*models.ItemModel{
days, err = strconv.Atoi(os.Args[1]) {"+5 Dexterity Vest", 10, 20},
if err != nil { {"Aged Brie", 2, 0},
fmt.Println(err.Error()) {"Elixir of the Mongoose", 5, 7},
os.Exit(1) {"Sulfuras, Hand of Ragnaros", 0, 80},
} {"Sulfuras, Hand of Ragnaros", -1, 80},
days++ {"Backstage passes to a TAFKAL80ETC concert", 15, 20},
} {"Backstage passes to a TAFKAL80ETC concert", 10, 49},
{"Backstage passes to a TAFKAL80ETC concert", 5, 49},
for day := 0; day < days; day++ { {"Conjured Mana Cake", 3, 6}, // <-- :O
fmt.Printf("-------- day %d --------\n", day) }
fmt.Println("Name, SellIn, Quality")
for i := 0; i < len(items); i++ { var items = make([]*models.Item, len(itemModels))
fmt.Println(items[i]) for i, item := range itemModels {
} items[i] = models.NewItem(item)
fmt.Println("") }
gildedrose.UpdateQuality(items)
} days := 2
var err error
if len(os.Args) > 2 {
days, err = strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
days++
}
for day := 0; day < days; day++ {
fmt.Printf("-------- day %d --------\n", day)
fmt.Println("name, sellIn, quality")
for i := 0; i < len(items); i++ {
fmt.Println(items[i].Model)
}
fmt.Println("")
for i := 0; i < len(items); i++ {
itemUpdateService := itemUpdateServiceProvider.GetUpdateService(items[i])
itemUpdateService.UpdateQuality(items[i])
}
}
}
}
func NewTextTestFixtureCommand() *TextTestFixtureCommand {
return &TextTestFixtureCommand{}
} }

66
go/docs/swagger.yaml Normal file
View File

@ -0,0 +1,66 @@
definitions:
controllers.UpdateQualityRequest:
properties:
days:
minimum: 0
type: integer
items:
items:
$ref: '#/definitions/models.ItemModel'
type: array
required:
- items
type: object
models.ItemModel:
properties:
name:
type: string
quality:
minimum: 0
type: integer
sellIn:
type: integer
required:
- name
- quality
- sellIn
type: object
info:
contact: {}
paths:
/status:
get:
consumes:
- application/json
description: Return a simple OK response
produces:
- application/json
responses:
"200":
description: Service is up and running
summary: Health check (get status)
tags:
- health
/update_quality:
post:
consumes:
- application/json
description: Load a full list of items that will be handled by the service and
process them, returning the item list with the updated values for the given
days
parameters:
- description: Items info
in: body
name: message
required: true
schema:
$ref: '#/definitions/controllers.UpdateQualityRequest'
responses:
"200":
description: Result of items with updated quality
"400":
description: Invalid data in the request
summary: Execute Update Quality for all given items and passed days
tags:
- items
swagger: "2.0"

View File

@ -1,58 +1,71 @@
package gildedrose package services
type Item struct { import (
Name string "github.com/emilybache/gildedrose-refactoring-kata/lib"
SellIn, Quality int "github.com/emilybache/gildedrose-refactoring-kata/models"
)
type ItemUpdateService struct {
logger lib.Logger
} }
func UpdateQuality(items []*Item) { func NewItemUpdateService(logger lib.Logger) ItemUpdateService {
for i := 0; i < len(items); i++ { return ItemUpdateService{
logger: logger,
if items[i].Name != "Aged Brie" && items[i].Name != "Backstage passes to a TAFKAL80ETC concert" { }
if items[i].Quality > 0 { }
if items[i].Name != "Sulfuras, Hand of Ragnaros" {
items[i].Quality = items[i].Quality - 1 func (this ItemUpdateService) UpdateQuality(item *models.Item) error {
} item.Mutex.Lock()
} defer item.Mutex.Unlock()
} else {
if items[i].Quality < 50 { itemModel := item.Model
items[i].Quality = items[i].Quality + 1
if items[i].Name == "Backstage passes to a TAFKAL80ETC concert" { if itemModel.Name != "Aged Brie" && itemModel.Name != "Backstage passes to a TAFKAL80ETC concert" {
if items[i].SellIn < 11 { if itemModel.Quality > 0 {
if items[i].Quality < 50 { if itemModel.Name != "Sulfuras, Hand of Ragnaros" {
items[i].Quality = items[i].Quality + 1 itemModel.Quality = itemModel.Quality - 1
} }
} }
if items[i].SellIn < 6 { } else {
if items[i].Quality < 50 { if itemModel.Quality < 50 {
items[i].Quality = items[i].Quality + 1 itemModel.Quality = itemModel.Quality + 1
} if itemModel.Name == "Backstage passes to a TAFKAL80ETC concert" {
} if itemModel.SellIn < 11 {
} if itemModel.Quality < 50 {
} itemModel.Quality = itemModel.Quality + 1
} }
}
if items[i].Name != "Sulfuras, Hand of Ragnaros" { if itemModel.SellIn < 6 {
items[i].SellIn = items[i].SellIn - 1 if itemModel.Quality < 50 {
} itemModel.Quality = itemModel.Quality + 1
}
if items[i].SellIn < 0 { }
if items[i].Name != "Aged Brie" { }
if items[i].Name != "Backstage passes to a TAFKAL80ETC concert" { }
if items[i].Quality > 0 { }
if items[i].Name != "Sulfuras, Hand of Ragnaros" {
items[i].Quality = items[i].Quality - 1 if itemModel.Name != "Sulfuras, Hand of Ragnaros" {
} itemModel.SellIn = itemModel.SellIn - 1
} }
} else {
items[i].Quality = items[i].Quality - items[i].Quality if itemModel.SellIn < 0 {
} if itemModel.Name != "Aged Brie" {
} else { if itemModel.Name != "Backstage passes to a TAFKAL80ETC concert" {
if items[i].Quality < 50 { if itemModel.Quality > 0 {
items[i].Quality = items[i].Quality + 1 if itemModel.Name != "Sulfuras, Hand of Ragnaros" {
} itemModel.Quality = itemModel.Quality - 1
} }
} }
} } else {
itemModel.Quality = itemModel.Quality - itemModel.Quality
}
} else {
if itemModel.Quality < 50 {
itemModel.Quality = itemModel.Quality + 1
}
}
}
return nil
} }