github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/examples/auto-configure/implementation/todos_impl.go (about)

     1  package implementation
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"sync"
     7  
     8  	"github.com/go-openapi/runtime/middleware"
     9  	"github.com/go-openapi/swag"
    10  	"github.com/go-swagger/go-swagger/examples/auto-configure/models"
    11  	"github.com/go-swagger/go-swagger/examples/auto-configure/restapi/operations/todos"
    12  )
    13  
    14  type TodosHandlerImpl struct {
    15  	// locks the entire struct
    16  	lock  sync.Mutex
    17  	items map[int64]*models.Item
    18  	// counter for newest item
    19  	idx int64
    20  }
    21  
    22  func (i *TodosHandlerImpl) AddOne(params todos.AddOneParams, principal interface{}) middleware.Responder {
    23  	i.lock.Lock()
    24  	defer i.lock.Unlock()
    25  	newItem := params.Body
    26  	if newItem == nil {
    27  		return todos.NewAddOneDefault(http.StatusBadRequest).
    28  			WithPayload(&models.Error{
    29  				Code:    http.StatusBadRequest,
    30  				Message: &[]string{"Item Body is nil"}[0],
    31  			})
    32  	}
    33  	// assign new id
    34  	newItem.ID = i.idx
    35  	i.idx++
    36  
    37  	i.items[newItem.ID] = newItem
    38  	return todos.NewAddOneCreated().WithPayload(newItem)
    39  }
    40  
    41  func (i *TodosHandlerImpl) DestroyOne(params todos.DestroyOneParams, principal interface{}) middleware.Responder {
    42  	i.lock.Lock()
    43  	defer i.lock.Unlock()
    44  	if _, ok := i.items[params.ID]; !ok {
    45  		return todos.NewDestroyOneDefault(http.StatusNotFound).
    46  			WithPayload(&models.Error{
    47  				Code:    http.StatusNotFound,
    48  				Message: &[]string{fmt.Sprintf("item with id %d is not found.", params.ID)}[0],
    49  			})
    50  	}
    51  	delete(i.items, params.ID)
    52  	return todos.NewDestroyOneNoContent()
    53  }
    54  
    55  func (i *TodosHandlerImpl) FindTodos(params todos.FindTodosParams, principal interface{}) middleware.Responder {
    56  	i.lock.Lock()
    57  	defer i.lock.Unlock()
    58  	mergedParams := todos.NewFindTodosParams()
    59  	mergedParams.Since = swag.Int64(0)
    60  	if params.Since != nil {
    61  		mergedParams.Since = params.Since
    62  	}
    63  	if params.Limit != nil {
    64  		mergedParams.Limit = params.Limit
    65  	}
    66  	limit := *mergedParams.Limit
    67  	since := *mergedParams.Since
    68  	// copy all items and return
    69  	result := make([]*models.Item, 0)
    70  	for id, item := range i.items {
    71  		if len(result) >= int(limit) {
    72  			break
    73  		}
    74  		if since == 0 || id > since {
    75  			result = append(result, item)
    76  		}
    77  	}
    78  	return todos.NewFindTodosOK().WithPayload(result)
    79  }
    80  
    81  func (i *TodosHandlerImpl) UpdateOne(params todos.UpdateOneParams, principal interface{}) middleware.Responder {
    82  	i.lock.Lock()
    83  	defer i.lock.Unlock()
    84  
    85  	if _, ok := i.items[params.ID]; !ok {
    86  		errStr := fmt.Sprintf("Item with id %v is not found", params.ID)
    87  		return todos.NewUpdateOneDefault(http.StatusNotFound).
    88  			WithPayload(&models.Error{
    89  				Code:    http.StatusNotFound,
    90  				Message: &errStr,
    91  			})
    92  	}
    93  	params.Body.ID = params.ID
    94  	i.items[params.ID] = params.Body
    95  
    96  	return todos.NewUpdateOneOK().WithPayload(params.Body)
    97  }