<% Class BoardsController_Class Private m_useLayout Private m_title Private Sub Class_Initialize() m_useLayout = True m_title = "Boards" End Sub Public Property Get useLayout() : useLayout = m_useLayout : End Property Public Property Let useLayout(v) : m_useLayout = v : End Property Public Property Get Title() : Title = m_title : End Property Public Property Let Title(v) : m_title = v : End Property ' GET /boards Public Sub Index() If Not KeycloakRequireLogin("") Then Exit Sub Dim boards : Set boards = boards_Repository().GetAll() %> <% Set boards = Nothing End Sub ' GET /boards/create Public Sub Create() If Not KeycloakRequireLogin("") Then Exit Sub m_title = "New Board" %> <% End Sub ' POST /boards Public Sub Store() If Not KeycloakRequireLogin("") Then Exit Sub Dim boardName, slug, currentUsername boardName = Trim(CStr(Request.Form("name"))) If Len(boardName) = 0 Then Dim flashCreateErr : Set flashCreateErr = Flash() flashCreateErr.AddError "Board name is required." MVC.RedirectTo "Boards", "Create" Exit Sub End If currentUsername = GetCurrentUsername() slug = boards_Repository().UniqueSlug(GenerateSlug(boardName), 0) Dim board : Set board = New POBO_boards board.name = boardName board.slug = slug board.created_at = Now() board.created_by = currentUsername board.updated_at = Now() board.updated_by = currentUsername boards_Repository().AddNew board Dim flashCreateOk : Set flashCreateOk = Flash() flashCreateOk.Success = "Board created." Response.Redirect "/board/" & board.slug End Sub ' GET /board/:slug Public Sub Show(slug) If Not KeycloakRequireLogin("") Then Exit Sub m_useLayout = False Dim board : Set board = boards_Repository().FindBySlug(slug) If board Is Nothing Then Response.Status = "404 Not Found" Response.Write "Board not found." Exit Sub End If m_title = board.name Dim columns : Set columns = board_columns_Repository().FindByBoardId(board.id) Dim lanes : Set lanes = swim_lanes_Repository().FindByBoardId(board.id) Dim allCards : Set allCards = cards_Repository().FindByBoardId(board.id) ' Build arrays for the view (functions cannot be defined inside a view include) Dim colCount : colCount = columns.Count Dim laneCount : laneCount = lanes.Count Dim colsArr() : ReDim colsArr(IIf(colCount > 0, colCount - 1, 0)) Dim lanesArr() : ReDim lanesArr(IIf(laneCount > 0, laneCount - 1, 0)) Dim colIdx, laneIdx, colIter, laneIter, colItem, laneItem colIdx = 0 Set colIter = columns.Iterator() Do While colIter.HasNext() Set colsArr(colIdx) = colIter.GetNext() colIdx = colIdx + 1 Loop laneIdx = 0 Set laneIter = lanes.Iterator() Do While laneIter.HasNext() Set lanesArr(laneIdx) = laneIter.GetNext() laneIdx = laneIdx + 1 Loop ' Serialise cards to JSON Dim cardsJson : cardsJson = "[" Dim firstCard : firstCard = True Dim cardIter, cardItem Set cardIter = allCards.Iterator() Do While cardIter.HasNext() Set cardItem = cardIter.GetNext() If Not firstCard Then cardsJson = cardsJson & "," cardsJson = cardsJson & "{""id"":" & cardItem.id & "," & _ """column_id"":" & cardItem.column_id & "," & _ """swim_lane_id"":" & cardItem.swim_lane_id & "," & _ """job_number"":" & JsonStr(cardItem.job_number) & "," & _ """job_name"":" & JsonStr(cardItem.job_name) & "," & _ """position"":" & cardItem.position & "}" firstCard = False Loop cardsJson = cardsJson & "]" %> <% Set board = Nothing Set columns = Nothing Set lanes = Nothing Set allCards = Nothing End Sub ' GET /board/:slug/edit Public Sub Edit(slug) If Not KeycloakRequireLogin("") Then Exit Sub m_title = "Edit Board" Dim board : Set board = boards_Repository().FindBySlug(slug) If board Is Nothing Then Response.Status = "404 Not Found" Response.Write "Board not found." Exit Sub End If %> <% Set board = Nothing End Sub ' POST /board/:slug/update Public Sub Update(slug) If Not KeycloakRequireLogin("") Then Exit Sub Dim board : Set board = boards_Repository().FindBySlug(slug) If board Is Nothing Then Response.Status = "404 Not Found" Response.Write "Board not found." Exit Sub End If Dim newName : newName = Trim(CStr(Request.Form("name"))) If Len(newName) = 0 Then Dim flashUpdateErr : Set flashUpdateErr = Flash() flashUpdateErr.AddError "Board name is required." Response.Redirect "/board/" & slug & "/edit" Exit Sub End If Dim newSlug : newSlug = boards_Repository().UniqueSlug(GenerateSlug(newName), CLng(board.id)) board.name = newName board.slug = newSlug board.updated_at = Now() board.updated_by = GetCurrentUsername() boards_Repository().Update board Dim flashUpdateOk : Set flashUpdateOk = Flash() flashUpdateOk.Success = "Board updated." Response.Redirect "/board/" & newSlug End Sub ' POST /board/:slug/delete Public Sub Destroy(slug) If Not KeycloakRequireLogin("") Then Exit Sub Dim board : Set board = boards_Repository().FindBySlug(slug) If board Is Nothing Then Response.Redirect "/boards" Exit Sub End If Dim boardId : boardId = CLng(board.id) cards_Repository().DeleteByBoardId boardId board_columns_Repository().DeleteByBoardId boardId swim_lanes_Repository().DeleteByBoardId boardId boards_Repository().Delete boardId Dim flashDeleteOk : Set flashDeleteOk = Flash() flashDeleteOk.Success = "Board deleted." MVC.RedirectTo "Boards", "Index" End Sub Private Function GetCurrentUsername() Dim u : Set u = KeycloakCurrentUser() Dim name : name = "" If Not u Is Nothing Then On Error Resume Next name = CStr(u.Item("preferred_username")) If Err.Number <> 0 Then name = "" Err.Clear End If On Error GoTo 0 End If GetCurrentUsername = name End Function Private Function JsonStr(s) Dim v : v = CStr(s) v = Replace(v, "\", "\\") v = Replace(v, """", "\""") v = Replace(v, vbCrLf, "\n") v = Replace(v, vbLf, "\n") v = Replace(v, vbCr, "\n") JsonStr = """" & v & """" End Function End Class Dim BoardsController_Class__Singleton Function BoardsController() If IsEmpty(BoardsController_Class__Singleton) Then Set BoardsController_Class__Singleton = New BoardsController_Class End If Set BoardsController = BoardsController_Class__Singleton End Function %>