summaryrefslogtreecommitdiffstats
path: root/app/preference.go
blob: 793f1802bc38ff52fe64e8a9b579ed4e0fc24a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package app

import (
	"github.com/mattermost/platform/model"
	"net/http"
)

func GetPreferencesForUser(userId string) (model.Preferences, *model.AppError) {
	if result := <-Srv.Store.Preference().GetAll(userId); result.Err != nil {
		result.Err.StatusCode = http.StatusBadRequest
		return nil, result.Err
	} else {
		return result.Data.(model.Preferences), nil
	}
}

func GetPreferenceByCategoryForUser(userId string, category string) (model.Preferences, *model.AppError) {
	if result := <-Srv.Store.Preference().GetCategory(userId, category); result.Err != nil {
		result.Err.StatusCode = http.StatusBadRequest
		return nil, result.Err
	} else if len(result.Data.(model.Preferences)) == 0 {
		err := model.NewAppError("getPreferenceCategory", "api.preference.preferences_category.get.app_error", nil, "", http.StatusNotFound)
		return nil, err
	} else {
		return result.Data.(model.Preferences), nil
	}
}

func GetPreferenceByCategoryAndNameForUser(userId string, category string, preferenceName string) (*model.Preference, *model.AppError) {
	if result := <-Srv.Store.Preference().Get(userId, category, preferenceName); result.Err != nil {
		result.Err.StatusCode = http.StatusBadRequest
		return nil, result.Err
	} else {
		data := result.Data.(model.Preference)
		return &data, nil
	}
}

func UpdatePreferences(preferences model.Preferences) (bool, *model.AppError) {
	if result := <-Srv.Store.Preference().Save(&preferences); result.Err != nil {
		result.Err.StatusCode = http.StatusBadRequest
		return false, result.Err
	}

	return true, nil
}

func DeletePreferences(userId string, preferences model.Preferences) (bool, *model.AppError) {
	for _, preference := range preferences {
		if result := <-Srv.Store.Preference().Delete(userId, preference.Category, preference.Name); result.Err != nil {
			result.Err.StatusCode = http.StatusBadRequest
			return false, result.Err
		}
	}

	return true, nil
}