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,17 +1,33 @@
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{}
func (this *TextTestFixtureCommand) Short() string {
return "execute texttest fixture"
}
func (this *TextTestFixtureCommand) Setup(command *cobra.Command) {}
func (this *TextTestFixtureCommand) Run() lib.CommandRunner {
return func(
env lib.Env,
logger lib.Logger,
itemUpdateServiceProvider domains.ItemUpdateServiceProvider,
) {
fmt.Println("OMGHAI!") fmt.Println("OMGHAI!")
var items = []*gildedrose.Item{ var itemModels = []*models.ItemModel{
{"+5 Dexterity Vest", 10, 20}, {"+5 Dexterity Vest", 10, 20},
{"Aged Brie", 2, 0}, {"Aged Brie", 2, 0},
{"Elixir of the Mongoose", 5, 7}, {"Elixir of the Mongoose", 5, 7},
@ -23,10 +39,15 @@ func main() {
{"Conjured Mana Cake", 3, 6}, // <-- :O {"Conjured Mana Cake", 3, 6}, // <-- :O
} }
var items = make([]*models.Item, len(itemModels))
for i, item := range itemModels {
items[i] = models.NewItem(item)
}
days := 2 days := 2
var err error var err error
if len(os.Args) > 1 { if len(os.Args) > 2 {
days, err = strconv.Atoi(os.Args[1]) days, err = strconv.Atoi(os.Args[2])
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
os.Exit(1) os.Exit(1)
@ -36,11 +57,19 @@ func main() {
for day := 0; day < days; day++ { for day := 0; day < days; day++ {
fmt.Printf("-------- day %d --------\n", day) fmt.Printf("-------- day %d --------\n", day)
fmt.Println("Name, SellIn, Quality") fmt.Println("name, sellIn, quality")
for i := 0; i < len(items); i++ { for i := 0; i < len(items); i++ {
fmt.Println(items[i]) fmt.Println(items[i].Model)
} }
fmt.Println("") fmt.Println("")
gildedrose.UpdateQuality(items) 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
} }
} }
} else {
if items[i].Quality < 50 { func (this ItemUpdateService) UpdateQuality(item *models.Item) error {
items[i].Quality = items[i].Quality + 1 item.Mutex.Lock()
if items[i].Name == "Backstage passes to a TAFKAL80ETC concert" { defer item.Mutex.Unlock()
if items[i].SellIn < 11 {
if items[i].Quality < 50 { itemModel := item.Model
items[i].Quality = items[i].Quality + 1
} if itemModel.Name != "Aged Brie" && itemModel.Name != "Backstage passes to a TAFKAL80ETC concert" {
} if itemModel.Quality > 0 {
if items[i].SellIn < 6 { if itemModel.Name != "Sulfuras, Hand of Ragnaros" {
if items[i].Quality < 50 { itemModel.Quality = itemModel.Quality - 1
items[i].Quality = items[i].Quality + 1 }
} }
} } else {
} if itemModel.Quality < 50 {
} itemModel.Quality = itemModel.Quality + 1
} if itemModel.Name == "Backstage passes to a TAFKAL80ETC concert" {
if itemModel.SellIn < 11 {
if items[i].Name != "Sulfuras, Hand of Ragnaros" { if itemModel.Quality < 50 {
items[i].SellIn = items[i].SellIn - 1 itemModel.Quality = itemModel.Quality + 1
} }
}
if items[i].SellIn < 0 { if itemModel.SellIn < 6 {
if items[i].Name != "Aged Brie" { if itemModel.Quality < 50 {
if items[i].Name != "Backstage passes to a TAFKAL80ETC concert" { itemModel.Quality = itemModel.Quality + 1
if items[i].Quality > 0 { }
if items[i].Name != "Sulfuras, Hand of Ragnaros" { }
items[i].Quality = items[i].Quality - 1 }
} }
} }
} else {
items[i].Quality = items[i].Quality - items[i].Quality if itemModel.Name != "Sulfuras, Hand of Ragnaros" {
} itemModel.SellIn = itemModel.SellIn - 1
} else { }
if items[i].Quality < 50 {
items[i].Quality = items[i].Quality + 1 if itemModel.SellIn < 0 {
} if itemModel.Name != "Aged Brie" {
} if itemModel.Name != "Backstage passes to a TAFKAL80ETC concert" {
} if itemModel.Quality > 0 {
} 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
} }