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 (
"fmt"
"os"
"strconv"
"fmt"
"os"
"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() {
fmt.Println("OMGHAI!")
type TextTestFixtureCommand struct{}
var items = []*gildedrose.Item{
{"+5 Dexterity Vest", 10, 20},
{"Aged Brie", 2, 0},
{"Elixir of the Mongoose", 5, 7},
{"Sulfuras, Hand of Ragnaros", 0, 80},
{"Sulfuras, Hand of Ragnaros", -1, 80},
{"Backstage passes to a TAFKAL80ETC concert", 15, 20},
{"Backstage passes to a TAFKAL80ETC concert", 10, 49},
{"Backstage passes to a TAFKAL80ETC concert", 5, 49},
{"Conjured Mana Cake", 3, 6}, // <-- :O
}
days := 2
var err error
if len(os.Args) > 1 {
days, err = strconv.Atoi(os.Args[1])
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])
}
fmt.Println("")
gildedrose.UpdateQuality(items)
}
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!")
var itemModels = []*models.ItemModel{
{"+5 Dexterity Vest", 10, 20},
{"Aged Brie", 2, 0},
{"Elixir of the Mongoose", 5, 7},
{"Sulfuras, Hand of Ragnaros", 0, 80},
{"Sulfuras, Hand of Ragnaros", -1, 80},
{"Backstage passes to a TAFKAL80ETC concert", 15, 20},
{"Backstage passes to a TAFKAL80ETC concert", 10, 49},
{"Backstage passes to a TAFKAL80ETC concert", 5, 49},
{"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
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 {
Name string
SellIn, Quality int
import (
"github.com/emilybache/gildedrose-refactoring-kata/lib"
"github.com/emilybache/gildedrose-refactoring-kata/models"
)
type ItemUpdateService struct {
logger lib.Logger
}
func UpdateQuality(items []*Item) {
for i := 0; i < len(items); i++ {
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 {
items[i].Quality = items[i].Quality + 1
if items[i].Name == "Backstage passes to a TAFKAL80ETC concert" {
if items[i].SellIn < 11 {
if items[i].Quality < 50 {
items[i].Quality = items[i].Quality + 1
}
}
if items[i].SellIn < 6 {
if items[i].Quality < 50 {
items[i].Quality = items[i].Quality + 1
}
}
}
}
}
if items[i].Name != "Sulfuras, Hand of Ragnaros" {
items[i].SellIn = items[i].SellIn - 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
}
}
} else {
items[i].Quality = items[i].Quality - items[i].Quality
}
} else {
if items[i].Quality < 50 {
items[i].Quality = items[i].Quality + 1
}
}
}
}
func NewItemUpdateService(logger lib.Logger) ItemUpdateService {
return ItemUpdateService{
logger: logger,
}
}
func (this ItemUpdateService) UpdateQuality(item *models.Item) error {
item.Mutex.Lock()
defer item.Mutex.Unlock()
itemModel := item.Model
if itemModel.Name != "Aged Brie" && 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 {
if itemModel.Quality < 50 {
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 itemModel.SellIn < 6 {
if itemModel.Quality < 50 {
itemModel.Quality = itemModel.Quality + 1
}
}
}
}
}
if itemModel.Name != "Sulfuras, Hand of Ragnaros" {
itemModel.SellIn = itemModel.SellIn - 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
}