eintopf.info@v0.13.16/service/notification/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 notification
    17  
    18  import (
    19  	"github.com/go-chi/chi/v5"
    20  
    21  	"eintopf.info/internal/crud"
    22  	"eintopf.info/internal/xhttp"
    23  	"eintopf.info/service/auth"
    24  )
    25  
    26  // Router returns a new http router that handles crud requests for
    27  // notifications and notification settings.
    28  func Router(storer Storer, settingsStorer SettingsStorer, authService auth.Service) func(r chi.Router) {
    29  	return func(r chi.Router) {
    30  		notificationRoutes(r, storer, authService)
    31  		r.Route("/settings", func(r chi.Router) {
    32  			settingsRoutes(r, settingsStorer, authService)
    33  		})
    34  	}
    35  }
    36  
    37  // swagger:response findNotificationsResponse
    38  type findResponse struct {
    39  	// in:body
    40  	Notifications []*Notification
    41  
    42  	// in:header
    43  	TotalNotifications int `json:"x-total-count"`
    44  }
    45  
    46  // swagger:parameters createNotification
    47  type createRequest struct {
    48  	// in:body
    49  	Notification *NewNotification
    50  }
    51  
    52  // swagger:response createNotificationResponse
    53  type createResponse struct {
    54  	// in:body
    55  	Notification *Notification
    56  }
    57  
    58  // swagger:parameters findNotification
    59  type findByIDRequest struct {
    60  	// in:query
    61  	ID string `json:"id"`
    62  }
    63  
    64  // swagger:response findNotificationResponse
    65  type findByIDResponse struct {
    66  	// in:body
    67  	Notification *Notification
    68  }
    69  
    70  type FindResponse[Model any] struct {
    71  	// in:body
    72  	Model *Model
    73  }
    74  
    75  // swagger:parameters updateNotification
    76  type updateRequest struct {
    77  	// in:body
    78  	Notification *Notification
    79  }
    80  
    81  // swagger:response updateNotificationResponse
    82  type updateResponse struct {
    83  	// in:body
    84  	Notification *Notification
    85  }
    86  
    87  // swagger:parameters deleteNotification
    88  type deleteRequest struct {
    89  	// in:query
    90  	ID string `json:"id"`
    91  }
    92  
    93  // swagger:response deleteNotificationResponse
    94  type deleteResponse struct{}
    95  
    96  func notificationRoutes(r chi.Router, storer Storer, authService auth.Service) {
    97  	handler := crud.NewHandler(storer)
    98  
    99  	r.Options("/", xhttp.CorsHandler)
   100  
   101  	// swagger:route GET /notifications/ notification findNotifications
   102  	//
   103  	// Retrieves all notifications.
   104  	//
   105  	// Parameters:
   106  	//   + name: offset
   107  	//     description: offset in notification list
   108  	//     in: query
   109  	//     type: integer
   110  	//     required: false
   111  	//   + name: limit
   112  	//     description: limits the notification list
   113  	//     in: query
   114  	//     type: integer
   115  	//     required: false
   116  	//   + name: sort
   117  	//     description: field that gets sorted
   118  	//     in: query
   119  	//     type: string
   120  	//     required: false
   121  	//   + name: order
   122  	//     description: sort order ("ASC" or "DESC")
   123  	//     in: query
   124  	//     type: string
   125  	//     required: false
   126  	//   + name: filters
   127  	//     description: filters get combined with AND logic
   128  	//     in: query
   129  	//     type: object
   130  	//     required: false
   131  	//
   132  	//     Responses:
   133  	//       200: findNotificationsResponse
   134  	//       400: badRequest
   135  	//       401: unauthorizedError
   136  	//       500: internalError
   137  	//       500: internalError
   138  	//       503: serviceUnavailable
   139  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   140  		Get("/", handler.Find)
   141  
   142  	// swagger:route POST /notifications/ notification createNotifcation
   143  	//
   144  	// Creates a new notification.
   145  	//     Security:
   146  	//       bearer: []
   147  	//
   148  	//     Responses:
   149  	//       200: createNotifcationResponse
   150  	//       400: badRequest
   151  	//       401: unauthorizedError
   152  	//       500: internalError
   153  	//       503: serviceUnavailable
   154  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   155  		Post("/", handler.Create)
   156  
   157  	r.Options("/{id}", xhttp.CorsHandler)
   158  
   159  	// swagger:route GET /notifications/{id} notification findNotification
   160  	//
   161  	// Finds the notification with the given id.
   162  	//
   163  	//     Responses:
   164  	//       200: findNotificationResponse
   165  	//       400: badRequest
   166  	//       401: unauthorizedError
   167  	//       500: internalError
   168  	//       503: serviceUnavailable
   169  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   170  		Get("/{id}", handler.FindByID)
   171  
   172  	// swagger:route PUT /notifications/{id} notification updateNotification
   173  	//
   174  	// Updates the notification with the given id.
   175  	//     Security:
   176  	//       bearer: []
   177  	//
   178  	//     Responses:
   179  	//       200: updateNotificationResponse
   180  	//       400: badRequest
   181  	//       401: unauthorizedError
   182  	//       500: internalError
   183  	//       503: serviceUnavailable
   184  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   185  		Put("/{id}", handler.Update)
   186  
   187  	// swagger:route DELETE /notifications/{id} notification deleteNotifications
   188  	//
   189  	// Deletes the notification with the given id.
   190  	//     Security:
   191  	//       bearer: []
   192  	//
   193  	//     Responses:
   194  	//       200: deleteNotificationResponse
   195  	//       400: badRequest
   196  	//       401: unauthorizedError
   197  	//       500: internalError
   198  	//       503: serviceUnavailable
   199  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   200  		Delete("/{id}", handler.Delete)
   201  }
   202  
   203  // swagger:response findNotificationSettingsResponse
   204  type findSettingsResponse struct {
   205  	// in:body
   206  	Settings []*Settings
   207  
   208  	// in:header
   209  	TotalSettings int `json:"x-total-count"`
   210  }
   211  
   212  // swagger:parameters createNotificationSettings
   213  type createSettingsRequest struct {
   214  	// in:body
   215  	Settings *NewSettings
   216  }
   217  
   218  // swagger:response createNotificationSettingsResponse
   219  type createSettingsResponse struct {
   220  	// in:body
   221  	Settings *Settings
   222  }
   223  
   224  // swagger:parameters findSettingsNotification
   225  type findSettingsByIDRequest struct {
   226  	// in:query
   227  	ID string `json:"id"`
   228  }
   229  
   230  // swagger:response findNotificationSettingsResponse
   231  type findSettingsByIDResponse struct {
   232  	// in:body
   233  	Settings *Settings
   234  }
   235  
   236  // swagger:parameters updateNotificationSettings
   237  type updateSettingsRequest struct {
   238  	// in:body
   239  	Settings *Settings
   240  }
   241  
   242  // swagger:response updateNotificationSettingsResponse
   243  type updateSettingsResponse struct {
   244  	// in:body
   245  	Settings *Settings
   246  }
   247  
   248  // swagger:parameters deleteNotificationSettings
   249  type deleteSettingsRequest struct {
   250  	// in:query
   251  	ID string `json:"id"`
   252  }
   253  
   254  // swagger:response deleteNotificationSettingsResponse
   255  type deleteSettingsResponse struct{}
   256  
   257  func settingsRoutes(r chi.Router, storer SettingsStorer, authService auth.Service) {
   258  	handler := crud.NewHandler(storer)
   259  	r.Options("/", xhttp.CorsHandler)
   260  
   261  	// swagger:route GET /notifications/settings/ notificationSetting findNotificationSettings
   262  	//
   263  	// Retrieves all notification settings.
   264  	//
   265  	// Parameters:
   266  	//   + name: offset
   267  	//     description: offset in the list
   268  	//     in: query
   269  	//     type: integer
   270  	//     required: false
   271  	//   + name: limit
   272  	//     description: limits the list
   273  	//     in: query
   274  	//     type: integer
   275  	//     required: false
   276  	//   + name: sort
   277  	//     description: field that gets sorted
   278  	//     in: query
   279  	//     type: string
   280  	//     required: false
   281  	//   + name: order
   282  	//     description: sort order ("ASC" or "DESC")
   283  	//     in: query
   284  	//     type: string
   285  	//     required: false
   286  	//   + name: filters
   287  	//     description: filters get combined with AND logic
   288  	//     in: query
   289  	//     type: object
   290  	//     required: false
   291  	//
   292  	//     Responses:
   293  	//       200: findNotificationSettingsResponse
   294  	//       400: badRequest
   295  	//       401: unauthorizedError
   296  	//       500: internalError
   297  	//       500: internalError
   298  	//       503: serviceUnavailable
   299  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: false})).
   300  		Get("/", handler.Find)
   301  
   302  	// swagger:route POST /notifications/settings/ notificationSetting createNotifcationSettings
   303  	//
   304  	// Creates a new notification settings.
   305  	//     Security:
   306  	//       bearer: []
   307  	//
   308  	//     Responses:
   309  	//       200: createNotifcationSettingsResponse
   310  	//       400: badRequest
   311  	//       401: unauthorizedError
   312  	//       500: internalError
   313  	//       503: serviceUnavailable
   314  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   315  		Post("/", handler.Create)
   316  
   317  	r.Options("/{id}", xhttp.CorsHandler)
   318  
   319  	// swagger:route GET /notifications/settings/{id} notificationSettings findNotificationSettings
   320  	//
   321  	// Finds the notification setting with the given id.
   322  	//
   323  	//     Responses:
   324  	//       200: findNotificationResponse
   325  	//       400: badRequest
   326  	//       401: unauthorizedError
   327  	//       500: internalError
   328  	//       503: serviceUnavailable
   329  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   330  		Get("/{id}", handler.FindByID)
   331  
   332  	// swagger:route PUT /notifications/settings/{id} notificationSettings updateNotificationSettings
   333  	//
   334  	// Updates the notifcation settings with the given id.
   335  	//     Security:
   336  	//       bearer: []
   337  	//
   338  	//     Responses:
   339  	//       200: updateNotificationSettingResponse
   340  	//       400: badRequest
   341  	//       401: unauthorizedError
   342  	//       500: internalError
   343  	//       503: serviceUnavailable
   344  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   345  		Put("/{id}", handler.Update)
   346  
   347  	// swagger:route DELETE /notifications/settings/{id} notificationSetting deleteNotificationSettings
   348  	//
   349  	// Deletes the notification settings with the given id.
   350  	//     Security:
   351  	//       bearer: []
   352  	//
   353  	//     Responses:
   354  	//       200: deleteNotificationSettingsResponse
   355  	//       400: badRequest
   356  	//       401: unauthorizedError
   357  	//       500: internalError
   358  	//       503: serviceUnavailable
   359  	r.With(auth.MiddlewareWithOpts(authService, auth.MiddlewareOpts{Validate: true})).
   360  		Delete("/{id}", handler.Delete)
   361  }