eintopf.info@v0.13.16/service/action/transport.go (about)

     1  // Copyright (C) 2024 The Eintopf authors
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  
    16  package action
    17  
    18  import (
    19  	"net/http"
    20  
    21  	"github.com/go-chi/chi/v5"
    22  
    23  	"eintopf.info/internal/crud"
    24  	"eintopf.info/internal/xhttp"
    25  	"eintopf.info/service/auth"
    26  )
    27  
    28  // Router returns a new http router that handles crud action requests for a given
    29  // action service.
    30  func Router(service *Service, authService auth.Service) func(chi.Router) {
    31  	handler := crud.NewHandler[Action, Action, Filters](service.Store())
    32  	return func(r chi.Router) {
    33  		r.Options("/", xhttp.CorsHandler)
    34  
    35  		// swagger:route GET /actions/ action findActions
    36  		//
    37  		// Retrieves all actions.
    38  		//
    39  		// Parameters:
    40  		//   + name: offset
    41  		//     description: offset in action list
    42  		//     in: query
    43  		//     type: integer
    44  		//     required: false
    45  		//   + name: limit
    46  		//     description: limits the action list
    47  		//     in: query
    48  		//     type: integer
    49  		//     required: false
    50  		//   + name: sort
    51  		//     description: field that gets sorted
    52  		//     in: query
    53  		//     type: string
    54  		//     required: false
    55  		//   + name: order
    56  		//     description: sort order ("ASC" or "DESC")
    57  		//     in: query
    58  		//     type: string
    59  		//     required: false
    60  		//   + name: filters
    61  		//     description: filters get combined with AND logic
    62  		//     in: query
    63  		//     type: object
    64  		//     required: false
    65  		//
    66  		//     Responses:
    67  		//       200: findActionsResponse
    68  		//       400: badRequest
    69  		//       401: unauthorizedError
    70  		//       500: internalError
    71  		//       500: internalError
    72  		//       503: serviceUnavailable
    73  		r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: false})).
    74  			Get("/", handler.Find)
    75  
    76  		// swagger:route POST /actions/ action createAction
    77  		//
    78  		// Creates a new action with the given data.
    79  		//     Security:
    80  		//       bearer: []
    81  		//
    82  		//     Responses:
    83  		//       200: createActionResponse
    84  		//       400: badRequest
    85  		//       401: unauthorizedError
    86  		//       500: internalError
    87  		//       503: serviceUnavailable
    88  		r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
    89  			Post("/", handler.Create)
    90  
    91  		r.Options("/{id}", xhttp.CorsHandler)
    92  
    93  		// swagger:route GET /actions/{id} action findAction
    94  		//
    95  		// Finds the action with the given id.
    96  		//
    97  		//     Responses:
    98  		//       200: findActionResponse
    99  		//       400: badRequest
   100  		//       401: unauthorizedError
   101  		//       500: internalError
   102  		//       503: serviceUnavailable
   103  		r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   104  			Get("/{id}", handler.FindByID)
   105  
   106  		// swagger:route PUT /actions/{id} action updateAction
   107  		//
   108  		// Updates the action with the given id.
   109  		//     Security:
   110  		//       bearer: []
   111  		//
   112  		//     Responses:
   113  		//       200: updateActionResponse
   114  		//       400: badRequest
   115  		//       401: unauthorizedError
   116  		//       500: internalError
   117  		//       503: serviceUnavailable
   118  		r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   119  			Put("/{id}", handler.Update)
   120  
   121  		// swagger:route DELETE /actions/{id} action deleteAction
   122  		//
   123  		// Deletes the action with the given id.
   124  		//     Security:
   125  		//       bearer: []
   126  		//
   127  		//     Responses:
   128  		//       200: deleteActionResponse
   129  		//       400: badRequest
   130  		//       401: unauthorizedError
   131  		//       500: internalError
   132  		//       503: serviceUnavailable
   133  		r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   134  			Delete("/{id}", handler.Delete)
   135  
   136  		// swagger:route POST /actions/run action runAction
   137  		//
   138  		// Runs the action with the given name.
   139  		//     Security:
   140  		//       bearer: []
   141  		//
   142  		//     Responses:
   143  		//       200: runActionResponse
   144  		//       400: badRequest
   145  		//       401: unauthorizedError
   146  		//       500: internalError
   147  		//       503: serviceUnavailable
   148  		r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   149  			Post("/run", func(w http.ResponseWriter, r *http.Request) {
   150  				req, err := xhttp.ReadJSON[RunRequest](r)
   151  				if err != nil {
   152  					xhttp.WriteBadRequest(r.Context(), w, err)
   153  				}
   154  				err = service.Call(r.Context(), req.Name)
   155  				if err != nil {
   156  					xhttp.WriteError(r.Context(), w, err)
   157  				}
   158  
   159  				xhttp.WriteJSON(r.Context(), w, RunResponse{})
   160  			})
   161  	}
   162  }
   163  
   164  // swagger:response findActionsResponse
   165  type FindResponse struct {
   166  	// in:body
   167  	Actions []*Action
   168  
   169  	// in:header
   170  	TotalActions int `json:"x-total-count"`
   171  }
   172  
   173  // swagger:parameters createAction
   174  type CreateRequest struct {
   175  	// in:body
   176  	Action *Action
   177  }
   178  
   179  // swagger:response createActionResponse
   180  type CreateResponse struct {
   181  	// in:body
   182  	Action *Action
   183  }
   184  
   185  // swagger:parameters findAction
   186  type FindByIDRequest struct {
   187  	// in:query
   188  	ID string `json:"id"`
   189  }
   190  
   191  // swagger:response findActionResponse
   192  type FindByIDResponse struct {
   193  	// in:body
   194  	Action *Action
   195  }
   196  
   197  // swagger:parameters updateAction
   198  type UpdateRequest struct {
   199  	// in:body
   200  	Action *Action
   201  }
   202  
   203  // swagger:response updateActionResponse
   204  type UpdateResponse struct {
   205  	// in:body
   206  	Action *Action
   207  }
   208  
   209  // swagger:parameters deleteAction
   210  type DeleteRequest struct {
   211  	// in:query
   212  	ID string `json:"id"`
   213  }
   214  
   215  // swagger:response deleteActionResponse
   216  type DeleteResponse struct{}
   217  
   218  // swagger:parameters deleteAction
   219  type RunRequest struct {
   220  	// in:body
   221  	Name string `json:"name"`
   222  }
   223  
   224  // swagger:response runActionResponse
   225  type RunResponse struct{}