Best Gauge code snippet using logger.logError
board.go
Source:board.go
...40// Create saves new Board to a repository and returns created Board.41func (i *BoardInteractor) Create(board model.Board) (model.Board, error) {42 board.ID = uuid.New().String()43 if err := i.validateBoard(board); err != nil {44 logError(i.logger, err)45 return model.Board{}, err46 }47 tx := i.txRepo.BeginTransaction(true)48 i.logger.Info(formatLogMsg(board.UserID, "Start transaction"))49 // Get last board50 boards, err := i.boardRepo.Find(tx, map[string]interface{}{51 "UserID": board.UserID,52 "After": "",53 })54 if err != nil {55 tx.Rollback()56 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))57 logError(i.logger, err)58 return model.Board{}, err59 }60 if len(boards) > 0 {61 lastBoard := boards[0]62 i.logger.Info(formatLogMsg(board.UserID, "Find last board("+lastBoard.ID+")"))63 board.Before = lastBoard.ID64 query := map[string]interface{}{65 "After": board.ID,66 }67 if err := i.boardRepo.Update(tx, lastBoard, query); err != nil {68 tx.Rollback()69 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))70 logError(i.logger, err)71 return model.Board{}, err72 }73 i.logger.Info(formatLogMsg(board.UserID, "Update board("+lastBoard.ID+")"))74 } else {75 i.logger.Info(formatLogMsg(board.UserID, "Find no board"))76 }77 if err := i.boardRepo.Create(tx, board); err != nil {78 tx.Rollback()79 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))80 logError(i.logger, err)81 return model.Board{}, err82 }83 i.logger.Info(formatLogMsg(board.UserID, "Create board("+board.ID+")"))84 tx.Commit()85 i.logger.Info(formatLogMsg(board.UserID, "Commit transaction"))86 return board, nil87}88// Delete removes Board in repository.89func (i *BoardInteractor) Delete(board model.Board) error {90 if board.ID == "" {91 i.logger.Info(formatLogMsg(board.UserID, "Invalid board. ID is empty"))92 return model.InvalidContentError{93 UserID: board.UserID,94 Err: nil,95 ID: "(No-ID)",96 Act: "validate board id",97 }98 }99 tx := i.txRepo.BeginTransaction(true)100 i.logger.Info(formatLogMsg(board.UserID, "Start transaction"))101 // Get board's info (e.g. board.Before, board.After...) and rewrite 'board'.102 board, err := i.boardRepo.FindByID(tx, board.ID, board.UserID)103 if err != nil {104 tx.Rollback()105 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))106 logError(i.logger, err)107 return err108 }109 i.logger.Info(formatLogMsg(board.UserID, "Find board("+board.ID+")"))110 if board.Before != "" {111 // Update board before deleting board112 before := model.Board{113 ID: board.Before,114 UserID: board.UserID,115 }116 query := map[string]interface{}{117 "After": board.After,118 }119 if err := i.boardRepo.Update(tx, before, query); err != nil {120 tx.Rollback()121 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))122 logError(i.logger, err)123 return err124 }125 i.logger.Info(formatLogMsg(board.UserID, "Update board("+before.ID+") before deleted board("+board.ID+")"))126 }127 if board.After != "" {128 // Update board after deleting board129 after := model.Board{130 ID: board.After,131 UserID: board.UserID,132 }133 query := map[string]interface{}{134 "Before": board.Before,135 }136 if err := i.boardRepo.Update(tx, after, query); err != nil {137 tx.Rollback()138 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))139 logError(i.logger, err)140 return err141 }142 i.logger.Info(formatLogMsg(board.UserID, "Update board("+after.ID+") after deleted board("+board.ID+")"))143 }144 if err := i.boardRepo.Delete(tx, board); err != nil {145 tx.Rollback()146 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))147 logError(i.logger, err)148 return err149 }150 i.logger.Info(formatLogMsg(board.UserID, "Delete board("+board.ID+")"))151 // Get lists in deleted board152 lists, err := i.listRepo.Find(tx, map[string]interface{}{153 "UserID": board.UserID,154 "BoardID": board.ID,155 })156 if err != nil {157 tx.Rollback()158 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))159 logError(i.logger, err)160 return err161 }162 i.logger.Info(formatLogMsg(board.UserID, "Find lists in deleted board("+board.ID+")"))163 for _, list := range lists {164 // Delete list165 if err := i.listRepo.Delete(tx, list); err != nil {166 tx.Rollback()167 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))168 logError(i.logger, err)169 return err170 }171 i.logger.Info(formatLogMsg(board.UserID, "Delete lists in deleted board("+board.ID+")"))172 items, err := i.itemRepo.Find(tx, map[string]interface{}{173 "UserID": list.UserID,174 "ListID": list.ID,175 })176 if err != nil {177 tx.Rollback()178 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))179 logError(i.logger, err)180 return err181 }182 i.logger.Info(formatLogMsg(board.UserID, "Find items in deleted list("+list.ID+")"))183 for _, item := range items {184 if err := i.itemRepo.Delete(tx, item); err != nil {185 tx.Rollback()186 i.logger.Info(formatLogMsg(item.UserID, "Rollback transaction"))187 logError(i.logger, err)188 return err189 }190 }191 i.logger.Info(formatLogMsg(board.UserID, "Delete items in deleted list("+list.ID+")"))192 }193 tx.Commit()194 i.logger.Info(formatLogMsg(board.UserID, "Commit transaction"))195 return nil196}197// Update replaces a Board and returns new Board.198func (i *BoardInteractor) Update(board model.Board) (model.Board, error) {199 if err := i.validateBoard(board); err != nil {200 logError(i.logger, err)201 return model.Board{}, err202 }203 query := map[string]interface{}{204 "Title": board.Title,205 "Text": board.Text,206 "Color": string(board.Color),207 }208 tx := i.txRepo.BeginTransaction(false)209 if err := i.boardRepo.Update(tx, board, query); err != nil {210 logError(i.logger, err)211 return model.Board{}, err212 }213 i.logger.Info(formatLogMsg(board.UserID, "Update board("+board.ID+")"))214 return board, nil215}216// Move moves Boards.217func (i *BoardInteractor) Move(board model.Board) error {218 board.Title = "dummy title"219 board.Color = model.RED220 if err := i.validateBoard(board); err != nil {221 logError(i.logger, err)222 return err223 }224 board.Title = ""225 tx := i.txRepo.BeginTransaction(true)226 i.logger.Info(formatLogMsg(board.UserID, "Start transaction"))227 // Get a board to move228 old, err := i.boardRepo.FindByID(tx, board.ID, board.UserID)229 if err != nil {230 tx.Rollback()231 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))232 logError(i.logger, err)233 return err234 }235 i.logger.Info(formatLogMsg(board.UserID, "Find board("+board.ID+") to move"))236 // Get a board before board to move237 if old.Before != "" {238 beforeOld := model.Board{239 ID: old.Before,240 UserID: old.UserID,241 }242 query := map[string]interface{}{243 "After": old.After,244 }245 if err := i.boardRepo.Update(tx, beforeOld, query); err != nil {246 tx.Rollback()247 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))248 logError(i.logger, err)249 return err250 }251 i.logger.Info(formatLogMsg(board.UserID, "Find a board("+beforeOld.ID+") before board("+board.ID+") to move"))252 }253 // Get a board after board to move254 if old.After != "" {255 afterOld := model.Board{256 ID: old.After,257 UserID: old.UserID,258 }259 query := map[string]interface{}{260 "Before": old.Before,261 }262 if err := i.boardRepo.Update(tx, afterOld, query); err != nil {263 tx.Rollback()264 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))265 logError(i.logger, err)266 return err267 }268 i.logger.Info(formatLogMsg(board.UserID, "Find a board("+afterOld.ID+") after board("+board.ID+") to move"))269 }270 // Get a board after moved board271 if board.Before == "" {272 conditions := map[string]interface{}{273 "UserID": board.UserID,274 "Before": "",275 }276 l, err := i.boardRepo.Find(tx, conditions)277 if err != nil {278 tx.Rollback()279 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))280 logError(i.logger, err)281 return err282 }283 if len(l) != 1 {284 tx.Rollback()285 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))286 err = model.ServerError{287 ID: board.ID,288 UserID: board.UserID,289 Err: nil,290 Act: "find a board before moved board",291 }292 logError(i.logger, err)293 return err294 }295 board.After = l[0].ID296 } else {297 before, err := i.boardRepo.FindByID(tx, board.Before, board.UserID)298 if err != nil {299 tx.Rollback()300 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))301 logError(i.logger, err)302 return err303 }304 board.After = before.After305 }306 i.logger.Info(formatLogMsg(board.UserID, "Find a board("+board.After+") after moved board("+board.ID+")"))307 // Update a board before moved board308 if board.Before != "" {309 before := model.Board{310 ID: board.Before,311 UserID: board.UserID,312 }313 query := map[string]interface{}{314 "After": board.ID,315 }316 if err := i.boardRepo.Update(tx, before, query); err != nil {317 tx.Rollback()318 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))319 logError(i.logger, err)320 return err321 }322 i.logger.Info(formatLogMsg(board.UserID, "Update board("+before.ID+") before board("+board.ID+")"))323 }324 // Update a board after moved board325 if board.After != "" {326 after := model.Board{327 ID: board.After,328 UserID: board.UserID,329 }330 query := map[string]interface{}{331 "Before": board.ID,332 }333 if err := i.boardRepo.Update(tx, after, query); err != nil {334 tx.Rollback()335 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))336 logError(i.logger, err)337 return err338 }339 i.logger.Info(formatLogMsg(board.UserID, "Update board("+after.ID+") after board("+board.ID+")"))340 }341 // Move board342 query := map[string]interface{}{343 "Before": board.Before,344 "After": board.After,345 }346 if err := i.boardRepo.Update(tx, board, query); err != nil {347 tx.Rollback()348 i.logger.Info(formatLogMsg(board.UserID, "Rollback transaction"))349 logError(i.logger, err)350 return err351 }352 i.logger.Info(formatLogMsg(board.UserID, "Move board("+board.ID+") after board("+board.Before+")"))353 tx.Commit()354 i.logger.Info(formatLogMsg(board.UserID, "Commit transaction"))355 return nil356}357// Get returns Board embedded all data.358func (i *BoardInteractor) Get(board model.Board) (model.Board, error) {359 tx := i.txRepo.BeginTransaction(false)360 board, err := i.boardRepo.FindByID(tx, board.ID, board.UserID)361 if err != nil {362 logError(i.logger, err)363 return model.Board{}, err364 }365 i.logger.Info(formatLogMsg(board.UserID, "Find board("+board.ID+")"))366 // Get Lists in Board.367 lists, err := i.listRepo.Find(tx, map[string]interface{}{368 "BoardID": board.ID,369 "UserID": board.UserID,370 })371 if err != nil {372 logError(i.logger, err)373 return model.Board{}, err374 }375 board.Lists = sortLists(lists)376 i.logger.Info(formatLogMsg(board.UserID, "Find lists in board("+board.ID+")"))377 // Get Items in Lists.378 for j, list := range board.Lists {379 items, err := i.itemRepo.Find(tx, map[string]interface{}{380 "ListID": list.ID,381 "UserID": list.UserID,382 })383 if err != nil {384 logError(i.logger, err)385 return model.Board{}, err386 }387 board.Lists[j].Items = sortItems(items)388 }389 i.logger.Info(formatLogMsg(board.UserID, "Find items in board("+board.ID+")"))390 i.logger.Info(formatLogMsg(board.UserID, "Get board("+board.ID+")"))391 return board, nil392}393// GetBoards returns User's Boards.394func (i *BoardInteractor) GetBoards(user model.User) (model.Boards, error) {395 tx := i.txRepo.BeginTransaction(false)396 boards, err := i.boardRepo.Find(tx, map[string]interface{}{397 "UserID": user.ID,398 })399 if err != nil {400 logError(i.logger, err)401 return model.Boards{}, err402 }403 i.logger.Info(formatLogMsg(user.ID, "Get boards"))404 return sortBoards(boards), nil405}406func (i *BoardInteractor) validateBoard(board model.Board) error {407 if board.ID == "" || board.Title == "" || board.UserID == "" {408 return model.InvalidContentError{409 UserID: board.UserID,410 Err: nil,411 ID: board.ID,412 Act: "validate contents in board",413 }414 }...
list.go
Source:list.go
...38// Create saves new List to a repository and returns created List.39func (i *ListInteractor) Create(list model.List) (model.List, error) {40 list.ID = uuid.New().String()41 if err := i.validateList(list); err != nil {42 logError(i.logger, err)43 return model.List{}, err44 }45 tx := i.txRepo.BeginTransaction(true)46 i.logger.Info(formatLogMsg(list.UserID, "Start transaction"))47 // Get last list in board48 lists, err := i.listRepo.Find(tx, map[string]interface{}{49 "BoardID": list.BoardID,50 "UserID": list.UserID,51 "After": "",52 })53 if err != nil {54 tx.Rollback()55 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))56 logError(i.logger, err)57 return model.List{}, err58 }59 if len(lists) > 0 {60 lastList := lists[0]61 i.logger.Info(formatLogMsg(list.UserID, "Find last list("+lastList.ID+") in board("+lastList.BoardID+")"))62 list.Before = lastList.ID63 query := map[string]interface{}{64 "After": list.ID,65 }66 if err := i.listRepo.Update(tx, lastList, query); err != nil {67 tx.Rollback()68 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))69 logError(i.logger, err)70 return model.List{}, err71 }72 i.logger.Info(formatLogMsg(list.UserID, "Update list("+lastList.ID+")"))73 } else {74 i.logger.Info(formatLogMsg(list.UserID, "Find no list in board("+list.BoardID+")"))75 }76 if err := i.listRepo.Create(tx, list); err != nil {77 tx.Rollback()78 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))79 logError(i.logger, err)80 return model.List{}, err81 }82 i.logger.Info(formatLogMsg(list.UserID, "Create list("+list.ID+")"))83 tx.Commit()84 i.logger.Info(formatLogMsg(list.UserID, "Commit transaction"))85 return list, nil86}87// Delete removes List in repository.88func (i *ListInteractor) Delete(list model.List) error {89 if list.ID == "" {90 i.logger.Info(formatLogMsg(list.UserID, "Invalid list. ID is empty"))91 return model.InvalidContentError{92 UserID: list.UserID,93 Err: nil,94 ID: "(No-ID)",95 Act: "validate list id",96 }97 }98 tx := i.txRepo.BeginTransaction(true)99 i.logger.Info(formatLogMsg(list.UserID, "Start transaction"))100 // Get list's info (e.g. list.Before, list.After...) and rewrite 'list'.101 list, err := i.listRepo.FindByID(tx, list.ID, list.UserID)102 if err != nil {103 tx.Rollback()104 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))105 logError(i.logger, err)106 return err107 }108 i.logger.Info(formatLogMsg(list.UserID, "Find list("+list.ID+")"))109 if list.Before != "" {110 // Update list before deleting list111 before := model.List{112 ID: list.Before,113 UserID: list.UserID,114 }115 query := map[string]interface{}{116 "After": list.After,117 }118 if err := i.listRepo.Update(tx, before, query); err != nil {119 tx.Rollback()120 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))121 logError(i.logger, err)122 return err123 }124 i.logger.Info(formatLogMsg(list.UserID, "Update list("+before.ID+") before deleted list("+list.ID+")"))125 }126 if list.After != "" {127 // Update list after deleting list128 after := model.List{129 ID: list.After,130 UserID: list.UserID,131 }132 query := map[string]interface{}{133 "Before": list.Before,134 }135 if err := i.listRepo.Update(tx, after, query); err != nil {136 tx.Rollback()137 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))138 logError(i.logger, err)139 return err140 }141 i.logger.Info(formatLogMsg(list.UserID, "Update list("+after.ID+") after deleted list("+list.ID+")"))142 }143 if err := i.listRepo.Delete(tx, list); err != nil {144 tx.Rollback()145 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))146 logError(i.logger, err)147 return err148 }149 i.logger.Info(formatLogMsg(list.UserID, "Delete list("+list.ID+")"))150 items, err := i.itemRepo.Find(tx, map[string]interface{}{151 "UserID": list.UserID,152 "ListID": list.ID,153 })154 if err != nil {155 tx.Rollback()156 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))157 logError(i.logger, err)158 return err159 }160 i.logger.Info(formatLogMsg(list.UserID, "Find items in deleted list("+list.ID+")"))161 for _, item := range items {162 if err := i.itemRepo.Delete(tx, item); err != nil {163 tx.Rollback()164 i.logger.Info(formatLogMsg(item.UserID, "Rollback transaction"))165 logError(i.logger, err)166 return err167 }168 }169 i.logger.Info(formatLogMsg(list.UserID, "Delete items in deleted list("+list.ID+")"))170 tx.Commit()171 i.logger.Info(formatLogMsg(list.UserID, "Commit transaction"))172 return nil173}174// Update replaces a List and returns new List.175func (i *ListInteractor) Update(list model.List) (model.List, error) {176 if err := i.validateList(list); err != nil {177 logError(i.logger, err)178 return model.List{}, err179 }180 query := map[string]interface{}{181 "Title": list.Title,182 }183 tx := i.txRepo.BeginTransaction(false)184 if err := i.listRepo.Update(tx, list, query); err != nil {185 logError(i.logger, err)186 return model.List{}, err187 }188 i.logger.Info(formatLogMsg(list.UserID, "Update list("+list.ID+")"))189 return list, nil190}191// Move moves Items.192func (i *ListInteractor) Move(list model.List) error {193 list.Title = "dummy title"194 if err := i.validateList(list); err != nil {195 logError(i.logger, err)196 return err197 }198 list.Title = ""199 tx := i.txRepo.BeginTransaction(true)200 i.logger.Info(formatLogMsg(list.UserID, "Start transaction"))201 // Get a list to move202 if list.Before != "" {203 before, err := i.listRepo.FindByID(tx, list.Before, list.UserID)204 if err != nil {205 tx.Rollback()206 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))207 logError(i.logger, err)208 return err209 }210 i.logger.Info(formatLogMsg(list.UserID, "Find a list("+before.ID+") before list to move"))211 if list.BoardID != before.BoardID {212 tx.Rollback()213 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))214 i.logger.Info(formatLogMsg(list.UserID, "Invalid list id("+list.BoardID+"). Does not equal before list's list id("+before.BoardID+")"))215 return err216 }217 }218 // Get a list to move219 old, err := i.listRepo.FindByID(tx, list.ID, list.UserID)220 if err != nil {221 tx.Rollback()222 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))223 logError(i.logger, err)224 return err225 }226 i.logger.Info(formatLogMsg(list.UserID, "Find list("+list.ID+") to move"))227 // Get a list before list to move228 if old.Before != "" {229 beforeOld := model.List{230 ID: old.Before,231 UserID: old.UserID,232 }233 query := map[string]interface{}{234 "After": old.After,235 }236 if err := i.listRepo.Update(tx, beforeOld, query); err != nil {237 tx.Rollback()238 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))239 logError(i.logger, err)240 return err241 }242 i.logger.Info(formatLogMsg(list.UserID, "Find a list("+beforeOld.ID+") before list("+list.ID+") to move"))243 }244 // Get a list after list to move245 if old.After != "" {246 afterOld := model.List{247 ID: old.After,248 UserID: old.UserID,249 }250 query := map[string]interface{}{251 "Before": old.Before,252 }253 if err := i.listRepo.Update(tx, afterOld, query); err != nil {254 tx.Rollback()255 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))256 logError(i.logger, err)257 return err258 }259 i.logger.Info(formatLogMsg(list.UserID, "Find a list("+afterOld.ID+") after list("+list.ID+") to move"))260 }261 // Get a list after moved list262 if list.Before == "" {263 conditions := map[string]interface{}{264 "BoardID": list.BoardID,265 "UserID": list.UserID,266 "Before": "",267 }268 l, err := i.listRepo.Find(tx, conditions)269 if err != nil {270 tx.Rollback()271 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))272 logError(i.logger, err)273 return err274 }275 if len(l) == 1 {276 list.After = l[0].ID277 } else if len(l) > 1 {278 tx.Rollback()279 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))280 err = model.ServerError{281 ID: list.ID,282 UserID: list.UserID,283 Err: nil,284 Act: "find a list before moved list",285 }286 logError(i.logger, err)287 return err288 }289 } else {290 before, err := i.listRepo.FindByID(tx, list.Before, list.UserID)291 if err != nil {292 tx.Rollback()293 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))294 logError(i.logger, err)295 return err296 }297 list.After = before.After298 }299 i.logger.Info(formatLogMsg(list.UserID, "Find a list("+list.After+") after moved list("+list.ID+")"))300 // Update a list before moved list301 if list.Before != "" {302 before := model.List{303 ID: list.Before,304 UserID: list.UserID,305 }306 query := map[string]interface{}{307 "After": list.ID,308 }309 if err := i.listRepo.Update(tx, before, query); err != nil {310 tx.Rollback()311 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))312 logError(i.logger, err)313 return err314 }315 i.logger.Info(formatLogMsg(list.UserID, "Update list("+before.ID+") before list("+list.ID+") in board("+list.BoardID+")"))316 }317 // Update a list after moved list318 if list.After != "" {319 after := model.List{320 ID: list.After,321 UserID: list.UserID,322 }323 query := map[string]interface{}{324 "Before": list.ID,325 }326 if err := i.listRepo.Update(tx, after, query); err != nil {327 tx.Rollback()328 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))329 logError(i.logger, err)330 return err331 }332 i.logger.Info(formatLogMsg(list.UserID, "Update list("+after.ID+") after list("+list.ID+") in board("+list.BoardID+")"))333 }334 // Move list335 query := map[string]interface{}{336 "BoardID": list.BoardID,337 "Before": list.Before,338 "After": list.After,339 }340 if err := i.listRepo.Update(tx, list, query); err != nil {341 tx.Rollback()342 i.logger.Info(formatLogMsg(list.UserID, "Rollback transaction"))343 logError(i.logger, err)344 return err345 }346 i.logger.Info(formatLogMsg(list.UserID, "Move list("+list.ID+") after list("+list.Before+") in board("+list.BoardID+")"))347 tx.Commit()348 i.logger.Info(formatLogMsg(list.UserID, "Commit transaction"))349 return nil350}351func (i *ListInteractor) validateList(list model.List) error {352 if list.ID == "" || list.Title == "" || list.BoardID == "" || list.UserID == "" {353 return model.InvalidContentError{354 UserID: list.UserID,355 Err: nil,356 ID: list.ID,357 Act: "validate contents in list",...
user_handler.go
Source:user_handler.go
1package handler2import (3 "net/http"4 "github.com/Dall06/go-cleanarch-template/pkg/infrastructure/services"5 "github.com/Dall06/go-cleanarch-template/pkg/infrastructure/middleware"6 "github.com/Dall06/go-cleanarch-template/pkg/internal/internal_user/delivery"7 "github.com/Dall06/go-cleanarch-template/pkg/internal/internal_user/usecase"8)9type UserHandler struct {10 UserInteractor *usecase.UserInteractor11 ResponseHandler *services.ResponseHandler12 JwtHandler *middleware.JWTHandler13 LoggerHandler *services.LoggerHandler14 UserHelper *delivery.UserHelper15}16func NewUserHandler(ai *usecase.UserInteractor) *UserHandler {17 return &UserHandler{18 UserInteractor: ai,19 ResponseHandler: services.NewResponseHandler(),20 JwtHandler: middleware.NewJWTHandler(),21 LoggerHandler: services.NewLoggerHandler(),22 UserHelper: delivery.NewUserHelper(),23 }24}25func (userHandler *UserHandler) IndexUser(w http.ResponseWriter, r *http.Request) { 26 email, err := userHandler.UserHelper.ValidateIndexRequest(r)27 if err != nil {28 userHandler.LoggerHandler.LogError("%s", err)29 userHandler.ResponseHandler.RespondWithBadRequest(err, w)30 }31 user, err := userHandler.UserInteractor.IndexUser(email)32 if err != nil {33 userHandler.LoggerHandler.LogError("%s", err)34 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)35 }36 userHandler.LoggerHandler.LogAccess("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)37 userHandler.ResponseHandler.RespondWithSuccess(user, w)38}39func (userHandler *UserHandler) IndexUserAndPlan(w http.ResponseWriter, r *http.Request) {40 email, err := userHandler.UserHelper.ValidateIndexRequest(r)41 if err != nil {42 userHandler.LoggerHandler.LogError("%s", err)43 userHandler.ResponseHandler.RespondWithBadRequest(err, w)44 }45 user, err := userHandler.UserInteractor.IndexUserAndPlan(email)46 if err != nil {47 userHandler.LoggerHandler.LogError("%s", err)48 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)49 }50 userHandler.LoggerHandler.LogAccess("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)51 userHandler.ResponseHandler.RespondWithSuccess(user, w)52}53func (userHandler *UserHandler) Save(w http.ResponseWriter, r *http.Request) {54 userToSave, err := userHandler.UserHelper.ValidateSaveRequest(r)55 if err != nil {56 userHandler.LoggerHandler.LogError("%s", err)57 userHandler.ResponseHandler.RespondWithBadRequest(err, w)58 }59 response, err := userHandler.UserInteractor.Save(&userToSave)60 if err != nil {61 userHandler.LoggerHandler.LogError("%s", err)62 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)63 }64 userHandler.LoggerHandler.LogAccess("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)65 userHandler.ResponseHandler.RespondWithSuccess(response, w)66}67func (userHandler *UserHandler) Change(w http.ResponseWriter, r *http.Request) {68 validated, err := userHandler.JwtHandler.ValidateTokenCookie(r)69 if err != nil {70 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)71 userHandler.LoggerHandler.LogError("%s", err)72 }73 if !validated {74 userHandler.ResponseHandler.RespondWithUnauthorized(err, w)75 userHandler.LoggerHandler.LogError("%s NO VALIDATED", err)76 }77 userToChange, newEmail, err := userHandler.UserHelper.ValidateChangeRequest(r)78 if err != nil {79 userHandler.LoggerHandler.LogError("%s", err)80 userHandler.ResponseHandler.RespondWithBadRequest(err, w)81 }82 response, err := userHandler.UserInteractor.Change(&userToChange, newEmail)83 if err != nil {84 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)85 userHandler.LoggerHandler.LogError("%s", err)86 }87 userHandler.LoggerHandler.LogAccess("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)88 userHandler.ResponseHandler.RespondWithSuccess(response, w)89}90func (userHandler *UserHandler) ChangePlan(w http.ResponseWriter, r *http.Request) {91 validated, err := userHandler.JwtHandler.ValidateTokenCookie(r)92 if err != nil {93 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)94 userHandler.LoggerHandler.LogError("%s", err)95 }96 if !validated {97 userHandler.ResponseHandler.RespondWithUnauthorized(err, w)98 userHandler.LoggerHandler.LogError("%s NO VALIDATED", err)99 }100 userToChange, err := userHandler.UserHelper.ValidateChangePlanRequest(r)101 if err != nil {102 userHandler.LoggerHandler.LogError("%s", err)103 userHandler.ResponseHandler.RespondWithBadRequest(err, w)104 }105 response, err := userHandler.UserInteractor.ChangePlan(&userToChange)106 if err != nil {107 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)108 userHandler.LoggerHandler.LogError("%s", err)109 }110 userHandler.LoggerHandler.LogAccess("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)111 userHandler.ResponseHandler.RespondWithSuccess(response, w)112}113func (userHandler *UserHandler) ChangePassword(w http.ResponseWriter, r *http.Request) {114 validated, err := userHandler.JwtHandler.ValidateTokenCookie(r)115 if err != nil {116 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)117 userHandler.LoggerHandler.LogError("%s", err)118 }119 if !validated {120 userHandler.ResponseHandler.RespondWithUnauthorized(err, w)121 userHandler.LoggerHandler.LogError("%s NO VALIDATED", err)122 }123 user, pass, err := userHandler.UserHelper.ValidateChangePasswordRequest(r)124 if err != nil {125 userHandler.LoggerHandler.LogError("%s", err)126 userHandler.ResponseHandler.RespondWithBadRequest(err, w)127 }128 response, err := userHandler.UserInteractor.ChangePassword(&user, pass)129 if err != nil {130 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)131 userHandler.LoggerHandler.LogError("%s", err)132 }133 userHandler.LoggerHandler.LogAccess("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)134 userHandler.ResponseHandler.RespondWithSuccess(response, w)135}136func (userHandler *UserHandler) Destroy(w http.ResponseWriter, r *http.Request) {137 validated, err := userHandler.JwtHandler.ValidateTokenCookie(r)138 if err != nil {139 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)140 userHandler.LoggerHandler.LogError("%s", err)141 }142 if !validated {143 userHandler.ResponseHandler.RespondWithUnauthorized(err, w)144 userHandler.LoggerHandler.LogError("%s NO VALIDATED", err)145 }146 user, err := userHandler.UserHelper.ValidateDestroyRequest(r)147 if err != nil {148 userHandler.LoggerHandler.LogError("%s", err)149 userHandler.ResponseHandler.RespondWithBadRequest(err, w)150 }151 response, err := userHandler.UserInteractor.Destroy(&user)152 if err != nil {153 userHandler.ResponseHandler.RespondWithInternalServerError(err, w)154 userHandler.LoggerHandler.LogError("%s", err)155 }156 userHandler.LoggerHandler.LogAccess("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)157 userHandler.ResponseHandler.RespondWithSuccess(response, w)158}...
logError
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World!")4 log.Println("Hello World!")5}6import (7func main() {8 fmt.Println("Hello World!")9 log.Println("Hello World!")10}11import (12func main() {13 fmt.Println("Hello World!")14 log.Println("Hello World!")15}
logError
Using AI Code Generation
1import (2func main() {3 logger.LogError("Error in main")4}5import (6func main() {7 logger.LogError("Error in main")8}9import (10func main() {11 logger.LogError("Error in main")12}
logError
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 logFile, err := os.Create("log.txt")5 if err != nil {6 panic(err)7 }8 defer logFile.Close()9 log.SetOutput(logFile)10 log.Println("This is a test log entry")11}12import (13func main() {14 logFile, err := os.Create("log.txt")15 if err != nil {16 panic(err)17 }18 defer logFile.Close()19 logger := log.New(logFile, "logger: ", log.Lshortfile)20 logger.Println("This is a test log entry")21}22import (23func main() {24 logFile, err := os.Create("log.txt")25 if err != nil {26 panic(err)27 }28 defer logFile.Close()29 logger := log.New(logFile, "logger: ", log.Ldate|log.Ltime|log.Lshortfile)30 logger.Println("This is a test log entry")31}32import (33func main() {34 logFile, err := os.Create("log.txt")35 if err != nil {36 panic(err)37 }38 defer logFile.Close()39 logger := log.New(logFile, "logger: ", log.Ldate|log.Ltime|log.Lshortfile)40 logger.SetPrefix("logger: ")41 logger.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)42 logger.Println("This is a test log entry")43}44import (45func main() {46 fmt.Println("Hello, playground")47 logFile, err := os.Create("log.txt")48 if err != nil {49 panic(err)50 }51 defer logFile.Close()52 logger := log.New(logFile, "logger: ", log.Ldate|log.Ltime|log.Lshortfile)53 logger.SetPrefix("logger: ")54 logger.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)55 logger.Println("This is a test
logError
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 logger.LogError("This is an error message")5}6import "fmt"7func LogError(message string) {8 fmt.Println("Error: ", message)9}
logError
Using AI Code Generation
1import (2func main(){3 logger := logger.NewLogger()4 logger.LogInfo("This is info")5 logger.LogDebug("This is debug")6 logger.LogError("This is error")7 logger.LogWarn("This is warn")8}9import (10func main(){11 logger := logger.NewLogger()12 logger.LogInfo("This is info")13 logger.LogDebug("This is debug")14 logger.LogError("This is error")15 logger.LogWarn("This is warn")16}17import (18func main(){19 logger := logger.NewLogger()20 logger.LogInfo("This is info")21 logger.LogDebug("This is debug")22 logger.LogError("This is error")23 logger.LogWarn("This is warn")24}
logError
Using AI Code Generation
1import (2func main() {3 fmt.Println("Inside main")4 logger.LogInfo("This is info message")5 logger.LogError("This is error message")6}7import (8func main() {9 fmt.Println("Inside main")10 logger.LogInfo("This is info message")11 logger.LogError("This is error message")12}13import (14func main() {15 fmt.Println("Inside main")16 logger.LogInfo("This is info message")17 logger.LogError("This is error message")18}19import (20func main() {21 fmt.Println("Inside main")22 logger.LogInfo("This is info message")23 logger.LogError("This is error message")24}25import (26func main() {27 fmt.Println("Inside main")28 logger.LogInfo("This is info message")29 logger.LogError("This is error message")30}31import (32func main() {33 fmt.Println("Inside main")34 logger.LogInfo("This is info message")35 logger.LogError("This is error message")36}37import (38func main() {39 fmt.Println("Inside main")40 logger.LogInfo("This is info message")41 logger.LogError("This is error message")42}43import (44func main() {45 fmt.Println("Inside main")46 logger.LogInfo("This is info message")47 logger.LogError("This is
logError
Using AI Code Generation
1import (2func main() {3 logger.LogInfo("info message")4 logger.LogError("error message")5}6import (7func main() {8 logger.LogInfo("info message")9 logger.LogError("error message")10}11import (12func main() {13 logger.LogInfo("info message")14 logger.LogError("error message")15}16import (17func main() {18 logger.LogInfo("info message")19 logger.LogError("error message")20}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!