github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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/thetreep/go-swagger/examples/auto-configure/models"
    11  	"github.com/thetreep/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(
    29  				&models.Error{
    30  					Code:    http.StatusBadRequest,
    31  					Message: &[]string{"Item Body is nil"}[0],
    32  				},
    33  			)
    34  	}
    35  	// assign new id
    36  	newItem.ID = i.idx
    37  	i.idx++
    38  
    39  	i.items[newItem.ID] = newItem
    40  	return todos.NewAddOneCreated().WithPayload(newItem)
    41  }
    42  
    43  func (i *TodosHandlerImpl) DestroyOne(params todos.DestroyOneParams, principal interface{}) middleware.Responder {
    44  	i.lock.Lock()
    45  	defer i.lock.Unlock()
    46  	if _, ok := i.items[params.ID]; !ok {
    47  		return todos.NewDestroyOneDefault(http.StatusNotFound).
    48  			WithPayload(
    49  				&models.Error{
    50  					Code:    http.StatusNotFound,
    51  					Message: &[]string{fmt.Sprintf("item with id %d is not found.", params.ID)}[0],
    52  				},
    53  			)
    54  	}
    55  	delete(i.items, params.ID)
    56  	return todos.NewDestroyOneNoContent()
    57  }
    58  
    59  func (i *TodosHandlerImpl) FindTodos(params todos.FindTodosParams, principal interface{}) middleware.Responder {
    60  	i.lock.Lock()
    61  	defer i.lock.Unlock()
    62  	mergedParams := todos.NewFindTodosParams()
    63  	mergedParams.Since = swag.Int64(0)
    64  	if params.Since != nil {
    65  		mergedParams.Since = params.Since
    66  	}
    67  	if params.Limit != nil {
    68  		mergedParams.Limit = params.Limit
    69  	}
    70  	limit := *mergedParams.Limit
    71  	since := *mergedParams.Since
    72  	// copy all items and return
    73  	result := make([]*models.Item, 0)
    74  	for id, item := range i.items {
    75  		if len(result) >= int(limit) {
    76  			break
    77  		}
    78  		if since == 0 || id > since {
    79  			result = append(result, item)
    80  		}
    81  	}
    82  	return todos.NewFindTodosOK().WithPayload(result)
    83  }
    84  
    85  func (i *TodosHandlerImpl) UpdateOne(params todos.UpdateOneParams, principal interface{}) middleware.Responder {
    86  	i.lock.Lock()
    87  	defer i.lock.Unlock()
    88  
    89  	if _, ok := i.items[params.ID]; !ok {
    90  		errStr := fmt.Sprintf("Item with id %v is not found", params.ID)
    91  		return todos.NewUpdateOneDefault(http.StatusNotFound).
    92  			WithPayload(
    93  				&models.Error{
    94  					Code:    http.StatusNotFound,
    95  					Message: &errStr,
    96  				},
    97  			)
    98  	}
    99  	params.Body.ID = params.ID
   100  	i.items[params.ID] = params.Body
   101  
   102  	return todos.NewUpdateOneOK().WithPayload(params.Body)
   103  }