refactor: replace if-else chains with switch statements for improved readability

This commit is contained in:
divyam234 2025-06-20 17:33:17 +05:30
parent 7b902778af
commit f48bc0fd27
No known key found for this signature in database
3 changed files with 15 additions and 13 deletions

View file

@ -12,6 +12,7 @@ import (
"math/big"
"net"
"net/http"
"slices"
"strconv"
"time"
@ -257,7 +258,8 @@ func (e *extendedService) AuthWs(w http.ResponseWriter, r *http.Request) {
conn.WriteJSON(map[string]any{"type": "auth", "payload": session, "message": "success"})
}()
case "phone":
if message.Message == "sendcode" {
switch message.Message {
case "sendcode":
go func() {
res, err := tgClient.Auth().SendCode(ctx, message.PhoneNo, tgauth.SendCodeOptions{})
if errors.Is(err, context.Canceled) {
@ -271,7 +273,7 @@ func (e *extendedService) AuthWs(w http.ResponseWriter, r *http.Request) {
code := res.(*tg.AuthSentCode)
conn.WriteJSON(map[string]any{"type": "auth", "payload": map[string]string{"phoneCodeHash": code.PhoneCodeHash}})
}()
} else if message.Message == "signin" {
case "signin":
go func() {
auth, err := tgClient.Auth().SignIn(ctx, message.PhoneNo, message.PhoneCode, message.PhoneCodeHash)
if errors.Is(err, context.Canceled) {
@ -389,11 +391,8 @@ func generateTgSession(dcId int, authKey []byte, port int) string {
func checkUserIsAllowed(allowedUsers []string, userName string) bool {
found := false
if len(allowedUsers) > 0 {
for _, user := range allowedUsers {
if user == userName {
found = true
break
}
if slices.Contains(allowedUsers, userName) {
found = true
}
} else {
found = true

View file

@ -273,10 +273,11 @@ func (a *apiService) FilesCreate(ctx context.Context, fileIn *api.File) (*api.Fi
}
if fileIn.Type == "folder" {
switch fileIn.Type {
case "folder":
fileDB.MimeType = "drive/folder"
fileDB.Parts = nil
} else if fileIn.Type == "file" {
case "file":
if fileIn.ChannelId.Value == 0 {
channelId, err = getDefaultChannel(a.db, a.cache, userId)
if err != nil {

View file

@ -29,9 +29,10 @@ var selectedFields = []string{"id", "name", "type", "mime_type", "category", "ch
func (afb *fileQueryBuilder) execute(filesQuery *api.FilesListParams, userId int64) (*api.FileList, error) {
query := afb.db.Where("user_id = ?", userId).Where("status = ?", filesQuery.Status.Value)
if filesQuery.Operation.Value == api.FileQueryOperationList {
switch filesQuery.Operation.Value {
case api.FileQueryOperationList:
query = afb.applyListFilters(query, filesQuery, userId)
} else if filesQuery.Operation.Value == api.FileQueryOperationFind {
case api.FileQueryOperationFind:
query = afb.applyFindFilters(query, filesQuery, userId)
}
@ -152,9 +153,10 @@ func (afb *fileQueryBuilder) applySingleDateFilter(query *gorm.DB, dateFilter st
}
func (afb *fileQueryBuilder) applySearchQuery(query *gorm.DB, filesQuery *api.FilesListParams) *gorm.DB {
if filesQuery.SearchType.Value == api.FileQuerySearchTypeText {
switch filesQuery.SearchType.Value {
case api.FileQuerySearchTypeText:
query = query.Where("name &@~ lower(regexp_replace(?, '[^[:alnum:]\\s]', ' ', 'g'))", filesQuery.Query.Value)
} else if filesQuery.SearchType.Value == api.FileQuerySearchTypeRegex {
case api.FileQuerySearchTypeRegex:
query = query.Where("name &~ ?", filesQuery.Query.Value)
}
return query