← back to egeozcan__mahresources

Function bodies 753 total

All specs Real LLM only Function bodies
application_context.buildNoteType function · go · L173-L186 (14 LOC)
application_context/crud_factories.go
func buildNoteType(editor *query_models.NoteTypeEditor) (models.NoteType, error) {
	if strings.TrimSpace(editor.Name) == "" {
		return models.NoteType{}, errors.New("note type name must be non-empty")
	}
	return models.NoteType{
		ID:            editor.ID,
		Name:          editor.Name,
		Description:   editor.Description,
		CustomHeader:  editor.CustomHeader,
		CustomSidebar: editor.CustomSidebar,
		CustomSummary: editor.CustomSummary,
		CustomAvatar:  editor.CustomAvatar,
	}, nil
}
application_context.MahresourcesContext.SeriesCRUD method · go · L189-L206 (18 LOC)
application_context/crud_factories.go
func (ctx *MahresourcesContext) SeriesCRUD() (
	*CRUDReader[models.Series, *query_models.SeriesQuery],
	*CRUDWriter[models.Series, *query_models.SeriesCreator],
) {
	reader := NewCRUDReader[models.Series, *query_models.SeriesQuery](ctx.db, CRUDReaderConfig[*query_models.SeriesQuery]{
		ScopeFn:       ScopeWithIgnoreSort(database_scopes.SeriesQuery),
		ScopeFnNoSort: ScopeWithIgnoreSortForCount(database_scopes.SeriesQuery),
		PreloadAssoc:  false,
	})

	writer := NewCRUDWriter[models.Series, *query_models.SeriesCreator](
		ctx.db,
		buildSeries,
		"series",
	)

	return reader, writer
}
application_context.buildSeries function · go · L208-L218 (11 LOC)
application_context/crud_factories.go
func buildSeries(creator *query_models.SeriesCreator) (models.Series, error) {
	name := strings.TrimSpace(creator.Name)
	if name == "" {
		return models.Series{}, errors.New("series name must be non-empty")
	}
	return models.Series{
		Name: name,
		Slug: name,
		Meta: []byte("{}"),
	}, nil
}
application_context.MahresourcesContext.NoteCRUDReader method · go · L222-L233 (12 LOC)
application_context/crud_factories.go
func (ctx *MahresourcesContext) NoteCRUDReader() *CRUDReader[models.Note, *query_models.NoteQuery] {
	return NewCRUDReader[models.Note, *query_models.NoteQuery](ctx.db, CRUDReaderConfig[*query_models.NoteQuery]{
		ScopeFn: func(query *query_models.NoteQuery) func(db *gorm.DB) *gorm.DB {
			return database_scopes.NoteQuery(query, false, ctx.db)
		},
		ScopeFnNoSort: func(query *query_models.NoteQuery) func(db *gorm.DB) *gorm.DB {
			return database_scopes.NoteQuery(query, true, ctx.db)
		},
		PreloadAssoc:   true,
		PreloadClauses: []string{"Tags", "NoteType"},
	})
}
application_context.MahresourcesContext.GroupCRUDReader method · go · L237-L249 (13 LOC)
application_context/crud_factories.go
func (ctx *MahresourcesContext) GroupCRUDReader() *CRUDReader[models.Group, *query_models.GroupQuery] {
	// Note: GroupQuery scope requires originalDB parameter and ignoreSort which we handle specially
	return NewCRUDReader[models.Group, *query_models.GroupQuery](ctx.db, CRUDReaderConfig[*query_models.GroupQuery]{
		ScopeFn: func(query *query_models.GroupQuery) func(db *gorm.DB) *gorm.DB {
			return database_scopes.GroupQuery(query, false, ctx.db)
		},
		ScopeFnNoSort: func(query *query_models.GroupQuery) func(db *gorm.DB) *gorm.DB {
			return database_scopes.GroupQuery(query, true, ctx.db)
		},
		PreloadAssoc:   true,
		PreloadClauses: []string{"Tags", "Category"},
	})
}
application_context.Q].Get method · go · L61-L68 (8 LOC)
application_context/generic_crud.go
func (r *CRUDReader[T, Q]) Get(id uint) (*T, error) {
	var entity T
	query := r.db
	if r.preloadAssoc {
		query = query.Preload(clause.Associations, pageLimit)
	}
	return &entity, query.First(&entity, id).Error
}
application_context.Q].List method · go · L71-L78 (8 LOC)
application_context/generic_crud.go
func (r *CRUDReader[T, Q]) List(offset, limit int, query Q) ([]T, error) {
	var entities []T
	dbQuery := r.db.Scopes(r.scopeFn(query))
	for _, preloadClause := range r.preloadClauses {
		dbQuery = dbQuery.Preload(preloadClause)
	}
	return entities, dbQuery.Limit(limit).Offset(offset).Find(&entities).Error
}
All rows above produced by Repobility · https://repobility.com
application_context.Q].GetByIDs method · go · L88-L100 (13 LOC)
application_context/generic_crud.go
func (r *CRUDReader[T, Q]) GetByIDs(ids []uint, limit int) ([]*T, error) {
	var entities []*T
	if len(ids) == 0 {
		return entities, nil
	}

	query := r.db
	if limit > 0 {
		query = query.Limit(limit)
	}

	return entities, query.Find(&entities, ids).Error
}
application_context.C].Create method · go · L125-L131 (7 LOC)
application_context/generic_crud.go
func (w *CRUDWriter[T, C]) Create(creator C) (*T, error) {
	entity, err := w.modelBuilder(creator)
	if err != nil {
		return nil, err
	}
	return &entity, w.db.Create(&entity).Error
}
application_context.MahresourcesContext.MergeGroups method · go · L19-L141 (123 LOC)
application_context/group_bulk_context.go
func (ctx *MahresourcesContext) MergeGroups(winnerId uint, loserIds []uint) error {
	if len(loserIds) == 0 {
		return errors.New("one or more losers required")
	}

	for _, id := range loserIds {
		if id == winnerId {
			return errors.New("winner cannot also be the loser")
		}
	}

	return ctx.WithTransaction(func(altCtx *MahresourcesContext) error {
		// Load losers WITHOUT associations — we only need their basic fields for backup
		var losers []*models.Group
		if loadErr := altCtx.db.Find(&losers, &loserIds).Error; loadErr != nil {
			return loadErr
		}

		// Load winner WITHOUT associations
		var winner models.Group
		if err := altCtx.db.First(&winner, winnerId).Error; err != nil {
			return err
		}

		// Batch SQL transfers — tags
		if err := altCtx.db.Exec("INSERT INTO group_tags (group_id, tag_id) SELECT ?, tag_id FROM group_tags WHERE group_id IN ? ON CONFLICT DO NOTHING", winnerId, loserIds).Error; err != nil {
			return err
		}

		// Batch SQL transfers — related groups (exclude
application_context.MahresourcesContext.BulkAddTagsToGroups method · go · L147-L173 (27 LOC)
application_context/group_bulk_context.go
func (ctx *MahresourcesContext) BulkAddTagsToGroups(query *query_models.BulkEditQuery) error {
	if len(query.ID) == 0 || len(query.EditedId) == 0 {
		return nil
	}

	uniqueEditedIds := deduplicateUints(query.EditedId)

	return ctx.db.Transaction(func(tx *gorm.DB) error {
		var tagCount int64
		if err := tx.Model(&models.Tag{}).Where("id IN ?", uniqueEditedIds).Count(&tagCount).Error; err != nil {
			return err
		}
		if int(tagCount) != len(uniqueEditedIds) {
			return fmt.Errorf("one or more tags not found")
		}

		for _, tagID := range uniqueEditedIds {
			if err := tx.Exec(
				"INSERT INTO group_tags (group_id, tag_id) SELECT id, ? FROM groups WHERE id IN ? ON CONFLICT DO NOTHING",
				tagID, query.ID,
			).Error; err != nil {
				return err
			}
		}
		return nil
	})
}
application_context.MahresourcesContext.BulkRemoveTagsFromGroups method · go · L175-L186 (12 LOC)
application_context/group_bulk_context.go
func (ctx *MahresourcesContext) BulkRemoveTagsFromGroups(query *query_models.BulkEditQuery) error {
	if len(query.ID) == 0 || len(query.EditedId) == 0 {
		return nil
	}

	return ctx.db.Transaction(func(tx *gorm.DB) error {
		return tx.Exec(
			"DELETE FROM group_tags WHERE group_id IN ? AND tag_id IN ?",
			query.ID, query.EditedId,
		).Error
	})
}
application_context.MahresourcesContext.BulkAddMetaToGroups method · go · L188-L206 (19 LOC)
application_context/group_bulk_context.go
func (ctx *MahresourcesContext) BulkAddMetaToGroups(query *query_models.BulkEditMetaQuery) error {
	if !json.Valid([]byte(query.Meta)) {
		return errors.New("invalid json")
	}

	var group models.Group
	var expr clause.Expr

	if ctx.Config.DbType == constants.DbTypePosgres {
		expr = gorm.Expr("meta || ?", query.Meta)
	} else {
		expr = gorm.Expr("json_patch(meta, ?)", query.Meta)
	}

	return ctx.db.
		Model(&group).
		Where("id in ?", query.ID).
		Update("Meta", expr).Error
}
application_context.MahresourcesContext.BulkDeleteGroups method · go · L208-L217 (10 LOC)
application_context/group_bulk_context.go
func (ctx *MahresourcesContext) BulkDeleteGroups(query *query_models.BulkQuery) error {
	return ctx.WithTransaction(func(altCtx *MahresourcesContext) error {
		for _, id := range query.ID {
			if err := altCtx.DeleteGroup(id); err != nil {
				return err
			}
		}
		return nil
	})
}
application_context.MahresourcesContext.FindParentsOfGroup method · go · L219-L251 (33 LOC)
application_context/group_bulk_context.go
func (ctx *MahresourcesContext) FindParentsOfGroup(id uint) ([]models.Group, error) {
	var results []models.Group
	var ids []uint

	findIdErr := ctx.db.Raw(`
		WITH RECURSIVE cte AS (
			SELECT id, owner_id, 1 AS level FROM groups WHERE id = ?
			UNION ALL
			SELECT g.id, g.owner_id, cte.level + 1 AS level FROM groups g
			INNER JOIN cte ON cte.owner_id = g.id
			WHERE cte.level < 20
		)
		SELECT id
		FROM cte
		ORDER BY level;
	`, id).Scan(&ids).Error

	if findIdErr != nil {
		return nil, findIdErr
	}

	findIdErr = ctx.db.Find(&results, ids).Error

	if findIdErr != nil {
		return nil, findIdErr
	}

	sort.Slice(results, func(i, j int) bool {
		return lib.IndexOf(ids, results[i].ID) > lib.IndexOf(ids, results[j].ID)
	})

	return results, nil
}
Repobility · code-quality intelligence · https://repobility.com
application_context.MahresourcesContext.DuplicateGroup method · go · L253-L288 (36 LOC)
application_context/group_bulk_context.go
func (ctx *MahresourcesContext) DuplicateGroup(id uint) (*models.Group, error) {
	var result *models.Group
	var original models.Group

	if err := ctx.db.Preload(clause.Associations).First(&original, id).Error; err != nil {
		return nil, err
	}

	// Copy slices to avoid shared references with the original
	relatedResources := make([]*models.Resource, len(original.RelatedResources))
	copy(relatedResources, original.RelatedResources)

	relatedNotes := make([]*models.Note, len(original.RelatedNotes))
	copy(relatedNotes, original.RelatedNotes)

	relatedGroups := make([]*models.Group, len(original.RelatedGroups))
	copy(relatedGroups, original.RelatedGroups)

	tags := make([]*models.Tag, len(original.Tags))
	copy(tags, original.Tags)

	result = &models.Group{
		Name:             original.Name,
		Description:      original.Description,
		URL:              original.URL,
		Meta:             original.Meta,
		OwnerId:          original.OwnerId,
		RelatedResources: relatedResources,
		RelatedNotes:
application_context.MahresourcesContext.CreateGroup method · go · L14-L81 (68 LOC)
application_context/group_crud_context.go
func (ctx *MahresourcesContext) CreateGroup(groupQuery *query_models.GroupCreator) (*models.Group, error) {
	if groupQuery.Name == "" {
		return nil, errors.New("group name needed")
	}

	if groupQuery.Meta == "" {
		groupQuery.Meta = "{}"
	}

	tx := ctx.db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	parsedURL, err := url.Parse(groupQuery.URL)

	if groupQuery.URL != "" && err != nil {
		return nil, err
	}

	groupUrl := (*types.URL)(parsedURL)

	group := models.Group{
		Name:        groupQuery.Name,
		Description: groupQuery.Description,
		CategoryId:  &groupQuery.CategoryId,
		Meta:        []byte(groupQuery.Meta),
		URL:         groupUrl,
	}

	if groupQuery.OwnerId != 0 {
		group.OwnerId = &groupQuery.OwnerId
	}

	if err := tx.Create(&group).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	if len(groupQuery.Tags) > 0 {
		tags := BuildAssociationSlice(groupQuery.Tags, TagFromID)

		if createTagsErr := tx.Model(&group).Association("Tags").
application_context.MahresourcesContext.UpdateGroup method · go · L83-L155 (73 LOC)
application_context/group_crud_context.go
func (ctx *MahresourcesContext) UpdateGroup(groupQuery *query_models.GroupEditor) (*models.Group, error) {
	if groupQuery.Name == "" {
		return nil, errors.New("group name needed")
	}

	tx := ctx.db.Begin()

	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	groups := BuildAssociationSlicePtr(groupQuery.Groups, GroupPtrFromID)
	tags := BuildAssociationSlicePtr(groupQuery.Tags, TagPtrFromID)

	if groupQuery.Meta == "" {
		groupQuery.Meta = "{}"
	}

	group := &models.Group{
		ID:          groupQuery.ID,
		Name:        groupQuery.Name,
		Description: groupQuery.Description,
		Meta:        []byte(groupQuery.Meta),
	}

	if groupQuery.URL != "" {
		parsedURL, err := url.Parse(groupQuery.URL)

		if groupQuery.URL != "" && err != nil {
			tx.Rollback()
			return nil, err
		}

		groupUrl := (*types.URL)(parsedURL)
		group.URL = groupUrl
	} else {
		group.URL = nil
	}

	if groupQuery.OwnerId != 0 {
		group.OwnerId = &groupQuery.OwnerId
		group.Owner = &models.Group{ID: g
application_context.MahresourcesContext.GetGroup method · go · L157-L179 (23 LOC)
application_context/group_crud_context.go
func (ctx *MahresourcesContext) GetGroup(id uint) (*models.Group, error) {
	var group models.Group

	err := ctx.db.
		Preload("OwnGroups", pageLimit).
		Preload("OwnResources", pageLimitCustom(5)).
		Preload("OwnNotes", pageLimit).
		Preload("RelatedResources", pageLimitCustom(5)).
		Preload("RelatedNotes", pageLimit).
		Preload("RelatedGroups", pageLimit).
		Preload("Tags").
		Preload("Owner").
		Preload("Category", pageLimit).
		Preload("Relationships").
		Preload("Relationships.ToGroup").
		Preload("Relationships.RelationType").
		Preload("BackRelations").
		Preload("BackRelations.FromGroup").
		Preload("BackRelations.RelationType").
		First(&group, id).Error

	return &group, err
}
application_context.MahresourcesContext.GetGroups method · go · L188-L194 (7 LOC)
application_context/group_crud_context.go
func (ctx *MahresourcesContext) GetGroups(offset, maxResults int, query *query_models.GroupQuery) ([]models.Group, error) {
	var groups []models.Group
	groupScope := database_scopes.GroupQuery(query, false, ctx.db)

	return groups, ctx.db.Scopes(groupScope).Limit(maxResults).
		Offset(offset).Preload("Tags").Preload("Category").Find(&groups).Error
}
application_context.MahresourcesContext.GetGroupsWithIds method · go · L196-L204 (9 LOC)
application_context/group_crud_context.go
func (ctx *MahresourcesContext) GetGroupsWithIds(ids *[]uint) ([]*models.Group, error) {
	var groups []*models.Group

	if len(*ids) == 0 {
		return groups, nil
	}

	return groups, ctx.db.Preload("Category").Find(&groups, ids).Error
}
application_context.MahresourcesContext.GetGroupsCount method · go · L206-L211 (6 LOC)
application_context/group_crud_context.go
func (ctx *MahresourcesContext) GetGroupsCount(query *query_models.GroupQuery) (int64, error) {
	var group models.Group
	var count int64

	return count, ctx.db.Scopes(database_scopes.GroupQuery(query, true, ctx.db)).Model(&group).Count(&count).Error
}
application_context.MahresourcesContext.GetPopularGroupTags method · go · L213-L226 (14 LOC)
application_context/group_crud_context.go
func (ctx *MahresourcesContext) GetPopularGroupTags(query *query_models.GroupQuery) ([]PopularTag, error) {
	var res []PopularTag

	db := ctx.db.Table("groups").
		Scopes(database_scopes.GroupQuery(query, true, ctx.db)).
		Joins("INNER JOIN group_tags pt ON pt.group_id = groups.id").
		Joins("INNER JOIN tags t ON t.id = pt.tag_id").
		Select("t.id AS id, t.name AS name, count(*) AS count").
		Group("t.id, t.name").
		Order("count DESC").
		Limit(20)

	return res, db.Scan(&res).Error
}
Source: Repobility analyzer · https://repobility.com
application_context.MahresourcesContext.DeleteGroup method · go · L228-L248 (21 LOC)
application_context/group_crud_context.go
func (ctx *MahresourcesContext) DeleteGroup(groupId uint) error {
	// Load group name before deletion for audit log
	var group models.Group
	if err := ctx.db.First(&group, groupId).Error; err != nil {
		return err
	}
	groupName := group.Name

	err := ctx.db.Transaction(func(tx *gorm.DB) error {
		ctx.EnsureForeignKeysActive(tx)

		return tx.
			Select("OwnGroups", "OwnNotes", "RelatedResources", "RelatedNotes", "RelatedGroups", "Relationships", "BackRelations", "Tags").
			Delete(&group).Error
	})
	if err == nil {
		ctx.Logger().Info(models.LogActionDelete, "group", &groupId, groupName, "Deleted group", nil)
		ctx.InvalidateSearchCacheByType(EntityTypeGroup)
	}
	return err
}
application_context.MahresourcesContext.GetGroupTreeRoots method · go · L8-L30 (23 LOC)
application_context/group_tree_context.go
func (ctx *MahresourcesContext) GetGroupTreeRoots(limit int) ([]query_models.GroupTreeNode, error) {
	if limit <= 0 || limit > 100 {
		limit = 50
	}

	var results []query_models.GroupTreeNode

	err := ctx.db.Raw(`
		SELECT g.id, g.name, g.owner_id, COALESCE(c.name, '') AS category_name,
		       (SELECT COUNT(*) FROM groups ch WHERE ch.owner_id = g.id) AS child_count
		FROM groups g
		LEFT JOIN categories c ON c.id = g.category_id
		WHERE g.owner_id IS NULL
		ORDER BY g.name
		LIMIT ?
	`, limit).Scan(&results).Error

	if err != nil {
		return nil, err
	}

	return results, nil
}
application_context.MahresourcesContext.GetGroupTreeChildren method · go · L33-L55 (23 LOC)
application_context/group_tree_context.go
func (ctx *MahresourcesContext) GetGroupTreeChildren(parentID uint, limit int) ([]query_models.GroupTreeNode, error) {
	if limit <= 0 || limit > 100 {
		limit = 50
	}

	var results []query_models.GroupTreeNode

	err := ctx.db.Raw(`
		SELECT g.id, g.name, g.owner_id, COALESCE(c.name, '') AS category_name,
		       (SELECT COUNT(*) FROM groups ch WHERE ch.owner_id = g.id) AS child_count
		FROM groups g
		LEFT JOIN categories c ON c.id = g.category_id
		WHERE g.owner_id = ?
		ORDER BY g.name
		LIMIT ?
	`, parentID, limit).Scan(&results).Error

	if err != nil {
		return nil, err
	}

	return results, nil
}
application_context.MahresourcesContext.GetGroupTreeDown method · go · L59-L117 (59 LOC)
application_context/group_tree_context.go
func (ctx *MahresourcesContext) GetGroupTreeDown(rootID uint, maxLevels int, childLimit int) ([]query_models.GroupTreeRow, error) {
	if maxLevels <= 0 {
		maxLevels = 3
	}
	if childLimit <= 0 || childLimit > 100 {
		childLimit = 50
	}

	var results []query_models.GroupTreeRow

	err := ctx.db.Raw(`
		WITH RECURSIVE tree AS (
			SELECT id, name, owner_id, category_id, 0 AS level
			FROM groups WHERE id = ?
			UNION ALL
			SELECT g.id, g.name, g.owner_id, g.category_id, tree.level + 1
			FROM groups g
			INNER JOIN tree ON g.owner_id = tree.id
			WHERE tree.level < ?
		)
		SELECT t.id, t.name, t.owner_id, COALESCE(c.name, '') AS category_name,
		       (SELECT COUNT(*) FROM groups ch WHERE ch.owner_id = t.id) AS child_count,
		       t.level
		FROM tree t
		LEFT JOIN categories c ON c.id = t.category_id
		ORDER BY t.level, t.name
		LIMIT 5000
	`, rootID, maxLevels).Scan(&results).Error

	if err != nil {
		return nil, err
	}

	// Enforce per-parent child limit in Go
	parentChildCount := ma
application_context.Logger.log method · go · L53-L85 (33 LOC)
application_context/log_context.go
func (l *Logger) log(level, action, entityType string, entityID *uint, entityName, message string, details map[string]interface{}) {
	entry := models.LogEntry{
		CreatedAt:  time.Now(),
		Level:      level,
		Action:     action,
		EntityType: entityType,
		EntityID:   entityID,
		EntityName: truncateString(entityName, 255),
		Message:    truncateString(message, 1000),
	}

	// Convert details to JSON if provided
	if details != nil {
		jsonBytes, err := json.Marshal(details)
		if err != nil {
			fmt.Printf("Logger: failed to marshal details: %v\n", err)
		} else {
			entry.Details = types.JSON(jsonBytes)
		}
	}

	// Capture HTTP request details if available
	if l.request != nil {
		entry.RequestPath = truncateString(l.request.URL.Path, 500)
		entry.UserAgent = truncateString(l.request.UserAgent(), 500)
		entry.IPAddress = getClientIP(l.request)
	}

	// Save to database (fire-and-forget, errors logged to stdout)
	if err := l.ctx.db.Create(&entry).Error; err != nil {
		fmt.Printf("Logger: 
application_context.MahresourcesContext.GetLogEntries method · go · L88-L94 (7 LOC)
application_context/log_context.go
func (ctx *MahresourcesContext) GetLogEntries(offset, maxResults int, query *query_models.LogEntryQuery) ([]models.LogEntry, error) {
	var logs []models.LogEntry
	return logs, ctx.db.Scopes(database_scopes.LogEntryQuery(query, false)).
		Limit(maxResults).
		Offset(offset).
		Find(&logs).Error
}
application_context.MahresourcesContext.GetLogEntriesCount method · go · L97-L102 (6 LOC)
application_context/log_context.go
func (ctx *MahresourcesContext) GetLogEntriesCount(query *query_models.LogEntryQuery) (int64, error) {
	var count int64
	return count, ctx.db.Model(&models.LogEntry{}).
		Scopes(database_scopes.LogEntryQuery(query, true)).
		Count(&count).Error
}
application_context.MahresourcesContext.GetEntityHistory method · go · L111-L122 (12 LOC)
application_context/log_context.go
func (ctx *MahresourcesContext) GetEntityHistory(entityType string, entityID uint, offset, limit int) ([]models.LogEntry, error) {
	var logs []models.LogEntry
	query := &query_models.EntityHistoryQuery{
		EntityType: entityType,
		EntityID:   entityID,
	}
	db := ctx.db.Scopes(database_scopes.EntityHistoryQuery(query))
	if limit > 0 {
		db = db.Limit(limit).Offset(offset)
	}
	return logs, db.Find(&logs).Error
}
Repobility analyzer · published findings · https://repobility.com
application_context.MahresourcesContext.GetEntityHistoryCount method · go · L125-L132 (8 LOC)
application_context/log_context.go
func (ctx *MahresourcesContext) GetEntityHistoryCount(entityType string, entityID uint) (int64, error) {
	var count int64
	query := &query_models.EntityHistoryQuery{
		EntityType: entityType,
		EntityID:   entityID,
	}
	return count, ctx.db.Model(&models.LogEntry{}).Scopes(database_scopes.EntityHistoryQuery(query)).Count(&count).Error
}
application_context.truncateString function · go · L146-L152 (7 LOC)
application_context/log_context.go
func truncateString(s string, maxLen int) string {
	if utf8.RuneCountInString(s) <= maxLen {
		return s
	}
	runes := []rune(s)
	return string(runes[:maxLen])
}
application_context.getClientIP function · go · L155-L183 (29 LOC)
application_context/log_context.go
func getClientIP(r *http.Request) string {
	// Check X-Forwarded-For header first (for proxied requests)
	if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
		// X-Forwarded-For can contain multiple IPs (client, proxy1, proxy2, ...)
		// Take only the first one (the original client)
		if idx := strings.Index(xff, ","); idx != -1 {
			xff = strings.TrimSpace(xff[:idx])
		}
		return truncateString(xff, 45)
	}

	// Check X-Real-IP header
	if xri := r.Header.Get("X-Real-IP"); xri != "" {
		return truncateString(xri, 45)
	}

	// Fall back to RemoteAddr, stripping the port if present
	ip := r.RemoteAddr
	// Handle IPv6 addresses like [::1]:8080
	if strings.HasPrefix(ip, "[") {
		if idx := strings.LastIndex(ip, "]"); idx != -1 {
			ip = ip[1:idx] // Extract IPv6 address without brackets
		}
	} else if idx := strings.LastIndex(ip, ":"); idx != -1 {
		// Handle IPv4 addresses like 192.168.1.1:8080
		ip = ip[:idx]
	}
	return truncateString(ip, 45)
}
mock_context.MockGroupContext.GetGroup method · go · L66-L72 (7 LOC)
application_context/mock_context/mock_group_context.go
func (MockGroupContext) GetGroup(id uint) (*models.Group, error) {
	return &models.Group{
		ID:        0,
		CreatedAt: time.Time{},
		UpdatedAt: time.Time{},
	}, nil
}
application_context.MahresourcesContext.CreateOrUpdateNote method · go · L17-L135 (119 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) CreateOrUpdateNote(noteQuery *query_models.NoteEditor) (*models.Note, error) {
	if noteQuery.Name == "" {
		return nil, errors.New("note name needed")
	}

	var note models.Note

	if noteQuery.Meta == "" {
		noteQuery.Meta = "{}"
	}

	var noteTypeId *uint
	if noteQuery.NoteTypeId != 0 {
		noteTypeId = &noteQuery.NoteTypeId
	}

	tx := ctx.db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if noteQuery.ID == 0 {

		note = models.Note{
			Name:        noteQuery.Name,
			Description: noteQuery.Description,
			Meta:        []byte(noteQuery.Meta),
			OwnerId:     &noteQuery.OwnerId,
			StartDate:   parseHTMLTime(noteQuery.StartDate),
			EndDate:     parseHTMLTime(noteQuery.EndDate),
			NoteTypeId:  noteTypeId,
		}

		if err := tx.Create(&note).Error; err != nil {
			tx.Rollback()
			return nil, err
		}

	} else {
		if err := tx.First(&note, noteQuery.ID).Error; err != nil {
			tx.Rollback()
			return nil, err
		}

		note.Name
application_context.MahresourcesContext.GetNote method · go · L137-L145 (9 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) GetNote(id uint) (*models.Note, error) {
	var note models.Note

	return &note, ctx.db.Preload(clause.Associations, pageLimit).
		Preload("Blocks", func(db *gorm.DB) *gorm.DB {
			return db.Order("position ASC")
		}).
		First(&note, id).Error
}
application_context.MahresourcesContext.GetNotes method · go · L147-L152 (6 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) GetNotes(offset, maxResults int, query *query_models.NoteQuery) ([]models.Note, error) {
	var notes []models.Note
	noteScope := database_scopes.NoteQuery(query, false, ctx.db)

	return notes, ctx.db.Scopes(noteScope).Limit(maxResults).Offset(offset).Preload("Tags").Preload("NoteType").Find(&notes).Error
}
application_context.MahresourcesContext.GetNotesWithIds method · go · L154-L162 (9 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) GetNotesWithIds(ids *[]uint) ([]*models.Note, error) {
	var notes []*models.Note

	if len(*ids) == 0 {
		return notes, nil
	}

	return notes, ctx.db.Find(&notes, ids).Error
}
All rows above produced by Repobility · https://repobility.com
application_context.MahresourcesContext.GetNoteCount method · go · L164-L169 (6 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) GetNoteCount(query *query_models.NoteQuery) (int64, error) {
	var note models.Note
	var count int64

	return count, ctx.db.Scopes(database_scopes.NoteQuery(query, true, ctx.db)).Model(&note).Count(&count).Error
}
application_context.MahresourcesContext.GetPopularNoteTags method · go · L171-L184 (14 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) GetPopularNoteTags(query *query_models.NoteQuery) ([]PopularTag, error) {
	var res []PopularTag

	db := ctx.db.Table("notes").
		Scopes(database_scopes.NoteQuery(query, true, ctx.db)).
		Joins("INNER JOIN note_tags pt ON pt.note_id = notes.id").
		Joins("INNER JOIN tags t ON t.id = pt.tag_id").
		Select("t.id AS id, t.name AS name, count(*) AS count").
		Group("t.id, t.name").
		Order("count DESC").
		Limit(20)

	return res, db.Scan(&res).Error
}
application_context.MahresourcesContext.DeleteNote method · go · L186-L200 (15 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) DeleteNote(noteId uint) error {
	// Load note name before deletion for audit log
	var note models.Note
	if err := ctx.db.First(&note, noteId).Error; err != nil {
		return err
	}
	noteName := note.Name

	err := ctx.db.Select(clause.Associations).Delete(&note).Error
	if err == nil {
		ctx.Logger().Info(models.LogActionDelete, "note", &noteId, noteName, "Deleted note", nil)
		ctx.InvalidateSearchCacheByType(EntityTypeNote)
	}
	return err
}
application_context.MahresourcesContext.ShareNote method · go · L202-L220 (19 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) ShareNote(noteId uint) (string, error) {
	var note models.Note
	if err := ctx.db.First(&note, noteId).Error; err != nil {
		return "", err
	}

	// If already shared, return existing token
	if note.ShareToken != nil {
		return *note.ShareToken, nil
	}

	token := lib.GenerateShareToken()
	if err := ctx.db.Model(&note).Update("share_token", token).Error; err != nil {
		return "", err
	}

	ctx.Logger().Info(models.LogActionUpdate, "note", &noteId, note.Name, "Created share token", nil)
	return token, nil
}
application_context.MahresourcesContext.UnshareNote method · go · L222-L234 (13 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) UnshareNote(noteId uint) error {
	var note models.Note
	if err := ctx.db.First(&note, noteId).Error; err != nil {
		return err
	}

	if err := ctx.db.Model(&note).Update("share_token", nil).Error; err != nil {
		return err
	}

	ctx.Logger().Info(models.LogActionUpdate, "note", &noteId, note.Name, "Removed share token", nil)
	return nil
}
application_context.MahresourcesContext.GetNoteByShareToken method · go · L236-L256 (21 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) GetNoteByShareToken(token string) (*models.Note, error) {
	if token == "" {
		return nil, errors.New("share token required")
	}

	var note models.Note
	err := ctx.db.
		Preload("Blocks", func(db *gorm.DB) *gorm.DB {
			return db.Order("position ASC")
		}).
		Preload("Resources").
		Preload("NoteType").
		Where("share_token = ?", token).
		First(&note).Error

	if err != nil {
		return nil, err
	}

	return &note, nil
}
application_context.MahresourcesContext.GetNoteTypesWithIds method · go · L273-L279 (7 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) GetNoteTypesWithIds(ids []uint) ([]models.NoteType, error) {
	var noteTypes []models.NoteType
	if len(ids) == 0 || (len(ids) == 1 && ids[0] == 0) {
		return noteTypes, nil
	}
	return noteTypes, ctx.db.Find(&noteTypes, ids).Error
}
application_context.MahresourcesContext.CreateOrUpdateNoteType method · go · L287-L317 (31 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) CreateOrUpdateNoteType(query *query_models.NoteTypeEditor) (*models.NoteType, error) {
	isNew := query.ID == 0
	var noteType models.NoteType
	if query.ID != 0 {
		if err := ctx.db.First(&noteType, query.ID).Error; err != nil {
			return nil, err
		}
	}
	if strings.TrimSpace(query.Name) != "" {
		noteType.Name = query.Name
	} else if isNew {
		return nil, errors.New("note type name must be non-empty")
	}
	noteType.Description = query.Description
	noteType.CustomHeader = query.CustomHeader
	noteType.CustomSidebar = query.CustomSidebar
	noteType.CustomSummary = query.CustomSummary
	noteType.CustomAvatar = query.CustomAvatar
	if err := ctx.db.Save(&noteType).Error; err != nil {
		return nil, err
	}

	if isNew {
		ctx.Logger().Info(models.LogActionCreate, "noteType", &noteType.ID, noteType.Name, "Created note type", nil)
	} else {
		ctx.Logger().Info(models.LogActionUpdate, "noteType", &noteType.ID, noteType.Name, "Updated note type", nil)
	}

	ctx.Invalidate
Repobility · code-quality intelligence · https://repobility.com
application_context.MahresourcesContext.DeleteNoteType method · go · L319-L333 (15 LOC)
application_context/note_context.go
func (ctx *MahresourcesContext) DeleteNoteType(noteTypeId uint) error {
	// Load note type name before deletion for audit log
	var noteType models.NoteType
	if err := ctx.db.First(&noteType, noteTypeId).Error; err != nil {
		return err
	}
	noteTypeName := noteType.Name

	err := ctx.db.Select(clause.Associations).Delete(&noteType).Error
	if err == nil {
		ctx.Logger().Info(models.LogActionDelete, "noteType", &noteTypeId, noteTypeName, "Deleted note type", nil)
		ctx.InvalidateSearchCacheByType(EntityTypeNoteType)
	}
	return err
}
application_context.MahresourcesContext.RunReadOnlyQuery method · go · L15-L23 (9 LOC)
application_context/query_context.go
func (ctx *MahresourcesContext) RunReadOnlyQuery(queryId uint, params map[string]any) (*sqlx.Rows, error) {
	var query models.Query

	if err := ctx.db.First(&query, queryId).Error; err != nil {
		return nil, err
	}

	return ctx.readOnlyDB.NamedQuery(strings.ReplaceAll(query.Text, "::", "::::"), params)
}
application_context.MahresourcesContext.RunReadOnlyQueryByName method · go · L25-L33 (9 LOC)
application_context/query_context.go
func (ctx *MahresourcesContext) RunReadOnlyQueryByName(queryName string, params map[string]any) (*sqlx.Rows, error) {
	var query models.Query

	if err := ctx.db.Where("name = ?", queryName).First(&query).Error; err != nil {
		return nil, err
	}

	return ctx.RunReadOnlyQuery(query.ID, params)
}
‹ prevpage 2 / 16next ›