summaryrefslogtreecommitdiff
path: root/lisp/service/profile-service.lisp
blob: e8b68de046a2f5a8ad2707293107ef7c7637c033 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(declaim (optimize (speed 0) (safety 3) (debug 3)))

(in-package :bogenherr)

(defclass profile-service (auth-service)
  ((title :initarg :title
          :initform nil
          :accessor title))
  (:documentation ""))

(defclass profile/view-service (profile-service)
  ((items :initarg items
          :initform nil
          :accessor items)
   (location-p :initform nil))
  (:documentation ""))

(defclass profile/modify-service (profile-service)
  ((form :initarg :form
         :initform nil
         :accessor form)
   (location-p :initform nil))
  (:documentation ""))

(defun profile-json ()
  (with-auth (instance profile-service "profile-modify")
    (setf (title instance) "Profile")))

(defun profile-view-json ()
  (with-auth (instance profile/view-service "profile-modify")
    (let ((user (get-user)))
      (setf (items instance) `(,user)))
    (sanitize-rest-json instance)
    (loop for item in (items instance)
          do (sanitize-json item))))

(defun profile-modify-json ()
  (with-auth (instance profile/modify-service "profile-modify")
    (let ((user (get-user)))
      (sanitize-json user)
      (setf (title instance) "Profile - Modify")
      (setf (form instance) (make-form "profile-modify-form"
                                       nil
                                       t
                                       `((:name "id" :label "" :field-type "hidden" :value ,(id user) :required "required")
                                         (:name "username" :label "Username" :field-type "text" :value ,(username user) :required "required")
                                         (:name "first_name" :label "First Name" :field-type "text" :value ,(first_name user) :required "required")
                                         (:name "last_name" :label "Last Name" :field-type "text" :value ,(last_name user) :required "required")
                                         (:name "email" :label "Email" :field-type "text" :value ,(email user) :required "required")
                                         (:name "phone" :label "Phone" :field-type "text" :value ,(phone user))
                                         (:label "Modify Profile" :field-type "button" :onclick "on_profile_modify_submit_clicked()")))))))

(defun profile-modify-submit-json (id username first_name last_name email phone)
  (declare (special username first_name last_name email phone))
  (with-auth (instance profile/modify-service "profile-modify")
    (with-bogenherr-database
      (let ((user (get-user)))
        (if (= (id user) id)
            (let ((auth-pkg (make-instance 'auth-pkg)))
              (loop for param in (remove-if (lambda (x)
                                              (intersection `(,x) '(id)))
                                            (sb-introspect:function-lambda-list #'profile-modify-submit-json))
                    do (setf (slot-value user param) (symbol-value param)))
              (update-user auth-pkg user)
              (set-user user)
              (setf (message instance) "Profile saved successfully."))
            (setf (errormsg instance) "An error occured."))))))

(define-endpoint ("/profile" :method :get) () profile-json)
(define-endpoint ("/profile/view" :method :get) () profile-view-json)
(define-endpoint ("/profile/modify" :method :post) () profile-modify-json)
(define-endpoint ("/profile/modify/submit" :method :post) (&post (id :parameter-type 'integer) (username :parameter-type 'string) (first_name :parameter-type 'string) (last_name :parameter-type 'string) (email :parameter-type 'string) (phone :parameter-type 'string)) profile-modify-submit-json id username first_name last_name email phone)