;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*- (declaim (optimize (speed 0) (safety 3) (debug 3))) (in-package :resume) ;; shared (eval-when (:compile-toplevel :load-toplevel :execute) (defun state-options (states) (mapcar (lambda (state) `(:label ,(format nil "~a ~a" (abbr state) (state state)) :value ,(id state))) states)) (defun visastatus-options (visastatuses) (mapcar (lambda (visastatus) `(:label ,(status visastatus) :value ,(id visastatus))) visastatuses)) (defun language-options () (mapcar (lambda (level) `(:label ,level :value ,level)) '("A1" "A2" "B1" "B2" "C1" "C2"))) (defun skill-checkboxes (resume-pkg user-id job-id) (let ((skills (get-records resume-pkg (make-instance 'skill :user_id user-id) "skill asc")) (job-skills (get-records resume-pkg (make-instance 'job-skill :job_id job-id) "skill_id asc"))) (mapcar (lambda (skill) (let ((checked (when (intersection `(,(id skill)) (mapcar (lambda (x) (skill_id x)) job-skills) :test '=) '(:checked "checked" :value "on")))) `(:name ,(format nil "chk_job_skill_link_~a" (id skill)) :label ,(skill skill) :field-type "checkbox" ,@checked))) skills))) (defun link-job (id skill-ids) (with-resume-database (let* ((resume-pkg (make-instance 'resume-pkg)) (skill-id-list (cl-ppcre:split "\\|" skill-ids))) (loop for skill-id in skill-id-list do (let* ((job-skill (make-instance 'job-skill :job_id id :skill_id skill-id))) (when (null (get-record resume-pkg job-skill)) (insert-record resume-pkg job-skill)))) (delete-records-where resume-pkg (make-instance 'job-skill) (format nil "job_id = ~a and skill_id not in (~a)" id (org-ckons-core::reduce-to-comma-separated-string skill-id-list)))))) (defun link-resume () ) (defclass resume/resume-service (auth-service) ((title :initarg :title :initform nil :accessor title)) (:documentation "")) (defclass resume/resume-component/view-service (resume/resume-service) ((components :initarg components :initform nil :accessor components) (location-p :initform nil)) (:documentation "")) (defclass resume/resume-component/modify-service (resume/resume-component/view-service) ((form :initarg :form :initform nil :accessor form)) (:documentation ""))) ;; macros (defmacro define-resume-component ((&key component-s rest-args link-rest-args add-fields modify-fields link-fields order-by view-p)) "`component-s' is a symbol that matches the component's record object name, i.e SKILL, JOB, EDUCATION, LANGUAGE, CONTACTINFO. This will be used all over the place. The record name equals the component name. `rest-args' is the list of REST API arguments for the add and modify forms. In the form like this example: ((id :parameter-type 'integer)) `link-rest-args' is the the same, but only for the linking form (job and resume). `add-fields', `modify-fields' and `link-fields' are lists of generic form fields that get inserted into the add, modify and link forms. If `link-fields' is `nil' then the linking form will not be included. `view-p' is `t' if the component has a view in the database because it needs to JOIN with other data." (let* ((component (symbol-name component-s)) (component-lcase (string-downcase component)) (component-s (intern (format nil "~a" component))) (table (format nil "resume.~a~a" component-lcase (if view-p "_v" ""))) (arg-names (loop for arg in rest-args collect (car arg))) (link-arg-names (loop for arg in link-rest-args collect (car arg))) (func-root-s (intern (format nil "~a-JSON" component))) (func-view-s (intern (format nil "~a-VIEW-JSON" component))) (func-add-s (intern (format nil "~a-ADD-JSON" component))) (func-add-submit-s (intern (format nil "~a-ADD-SUBMIT-JSON" component))) (func-modify-s (intern (format nil "~a-MODIFY-JSON" component))) (func-modify-submit-s (intern (format nil "~a-MODIFY-SUBMIT-JSON" component))) (func-link-s (intern (format nil "~a-LINK-JSON" component))) (func-link-submit-s (intern (format nil "~a-LINK-SUBMIT-JSON" component))) (func-delete-s (intern (format nil "~a-DELETE-JSON" component))) (form-add (format nil "~a-add-form" component-lcase)) (form-modify (format nil "~a-modify-form" component-lcase)) (form-link (format nil "~a-link-form" component-lcase))) `(progn (defun ,func-root-s () (with-auth (instance resume/resume-service "resume-view") (setf (title instance) ,component))) (defun ,func-view-s () (with-auth (instance resume/resume-component/view-service "resume-view") (with-resume-database (let ((resume-pkg (make-instance 'resume-pkg)) (user (get-user))) (setf (components instance) (get-records resume-pkg (make-instance ',component-s :user_id (id user) :*table ,table) ,order-by)))) (sanitize-rest-json instance) (loop for component in (components instance) do (sanitize-json component)))) (defun ,func-add-s () (with-auth (instance resume/resume-component/modify-service "resume-modify") (with-resume-database (let* ((resume-pkg (make-instance 'resume-pkg))) (setf (title instance) (format nil "~a - Add" ,component)) (setf (form instance) (make-form ,form-add nil t ,add-fields)))))) (defun ,func-add-submit-s (,@arg-names) (declare (special ,@arg-names)) (with-auth (instance resume/resume-component/modify-service "resume-modify") (with-resume-database (handler-case (let* ((resume-pkg (make-instance 'resume-pkg)) (user (get-user)) (,component-s (make-instance ',component-s :user_id (id user)))) (loop for param in (remove-if (lambda (x) (intersection `(,x) '(id))) (sb-introspect:function-lambda-list #',func-add-submit-s)) do (setf (slot-value ,component-s param) (symbol-value param))) (insert-record resume-pkg ,component-s) (setf (session-value :message) (format nil "~a created successfully." ,component))) (error (e) (setf (session-value :errormsg) (format nil "Error creating ~a. ~a" ,component e))))))) (defun ,func-modify-s (id) (with-auth (instance resume/resume-component/modify-service "resume-modify") (setf (title instance) (format nil "~a - Modify" ,component)) (with-resume-database (let* ((resume-pkg (make-instance 'resume-pkg)) (user (get-user)) (,component-s (get-record resume-pkg (make-instance ',component-s :id id :user_id (id user) :*table ,table)))) (if ,component-s (setf (form instance) (make-form ,form-modify nil t ,modify-fields)) (setf (session-value :errormsg) (format nil "Error: could not modify ~a. Not found." ,component))))))) (defun ,func-modify-submit-s (,@(append '(id) arg-names)) (declare (special ,@(append '(id) arg-names))) (with-auth (instance resume/resume-component/modify-service "resume-modify") (with-resume-database (handler-case (let* ((resume-pkg (make-instance 'resume-pkg)) (user (get-user)) (,component-s (get-record resume-pkg (make-instance ',component-s :id id :user_id (id user) :*table ,table)))) (if ,component-s (let ((new-component (make-instance ',component-s))) (loop for param in (sb-introspect:function-lambda-list #',func-modify-submit-s) do (setf (slot-value new-component param) (symbol-value param))) (setf (user_id new-component) (id user)) (update-record-no-nulls resume-pkg new-component) (setf (session-value :message) (format nil "~a modified successfully." ,component)) (setf (message instance) (session-value :message))) (error (format nil "~a error" ,component)))) (error (e) (setf (session-value :errormsg) (format nil "Error modifying ~a. ~a" ,component e)) (setf (errormsg instance) (session-value :errormsg))))))) (defun ,func-link-s (&optional id) (with-auth (instance resume/resume-component/modify-service "resume-modify") (setf (title instance) (format nil "~a - Link" ,component)) (when (> (length ',link-arg-names) 0) (with-resume-database (let* ((resume-pkg (make-instance 'resume-pkg)) (user (get-user)) (,component-s (get-record resume-pkg (make-instance ',component-s :id id :user_id (id user) :*table ,table)))) (if ,component-s (setf (form instance) (make-form ,form-link nil t ,link-fields)) (setf (session-value :errormsg) (format nil "Error: could not link ~a. Not found." ,component)))))))) (defun ,func-link-submit-s (&rest args) (with-auth (instance resume/resume-component/modify-service "resume-modify") (handler-case (progn (apply `,(intern (format nil "LINK-~a" ,component) (intern (package-name #.*package*))) args) (setf (message instance) (format nil "~a linked successfully." ,component))) (error (e) (setf (errormsg instance) (format nil "Error linking ~a. ~a" ,component e)))))) (defun ,func-delete-s (id) (with-auth (instance resume/resume-component/modify-service "resume-modify") (with-resume-database (let* ((resume-pkg (make-instance 'resume-pkg)) (user (get-user)) (,component-s (get-record resume-pkg (make-instance ',component-s :id id :user_id (id user) :*table ,table)))) (if ,component-s (progn (delete-record resume-pkg (make-instance ',component-s :id id)) (setf (session-value :message) (format nil "~a deleted successfully." ,component)) (setf (message instance) (session-value :message))) (progn (setf (session-value :errormsg) (format nil "Error: could not delete ~a: not found." ,component)) (setf (errormsg instance) (session-value :errormsg)))))))) (define-endpoint (,(format nil "/resume/~a" component-lcase) :method :get) () ,func-root-s) (define-endpoint (,(format nil "/resume/~a/view" component-lcase) :method :get) () ,func-view-s) (define-endpoint (,(format nil "/resume/~a/add" component-lcase) :method :post) () ,func-add-s) (define-endpoint (,(format nil "/resume/~a/add/submit" component-lcase) :method :post) (&post ,@rest-args) ,func-add-submit-s ,@arg-names) (define-endpoint (,(format nil "/resume/~a/modify" component-lcase) :method :post) (&post (id :parameter-type 'integer)) ,func-modify-s id) (define-endpoint (,(format nil "/resume/~a/modify/submit" component-lcase) :method :post) (&post (id :parameter-type 'integer) ,@rest-args) ,func-modify-submit-s ,@(append '(id) arg-names)) (define-endpoint (,(format nil "/resume/~a/link" component-lcase) :method :post) (&post (id :parameter-type 'integer)) ,func-link-s id) (define-endpoint (,(format nil "/resume/~a/link/submit" component-lcase) :method :post) (&post (id :parameter-type 'integer) ,@link-rest-args) ,func-link-submit-s ,@(append '(id) link-arg-names)) (define-endpoint (,(format nil "/resume/~a/delete/:id" component-lcase) :method :delete) (&path (id 'integer)) ,func-delete-s id)))) (define-resume-component (:component-s resume :rest-args ((name :parameter-type 'string)) :add-fields `((:name "name" :label "Name" :field-type "text" :required "required") (:label "Add RESUME" :field-type "button" :onclick "on_resume_add_submit_clicked()")) :modify-fields `((:name "id" :field-type "hidden" :value ,(id resume) :required "required") (:name "name" :label "Name" :field-type "text" :value ,(name resume) :required "required") (:label "Modify RESUME" :field-type "button" :onclick "on_resume_modify_submit_clicked()")) :order-by "name asc")) (define-resume-component (:component-s job :rest-args ((company :parameter-type 'string) (location :parameter-type 'string) (start_date :parameter-type 'string) (end_date :parameter-type 'string) (overview :parameter-type 'string)) :link-rest-args ((skill :parameter-type 'string)) :add-fields `((:name "company" :label "Company" :field-type "text" :required "required") (:name "location" :label "Location" :field-type "text" :required "required") (:name "start_date" :label "Start Date" :field-type "text" :required "required") (:name "end_date" :label "End Date" :field-type "text" :required "required") (:name "overview" :label "Overview" :field-type "textarea" :required "required") (:label "Add Job" :field-type "button" :onclick "on_job_add_submit_clicked()")) :modify-fields `((:name "id" :field-type "hidden" :value ,(id job) :required "required") (:name "company" :label "Company" :field-type "text" :value ,(company job) :required "required") (:name "location" :label "Location" :field-type "text" :value ,(location job) :required "required") (:name "start_date" :label "Start Date" :field-type "text" :value ,(start_date job) :required "required") (:name "end_date" :label "End Date" :field-type "text" :value ,(end_date job) :required "required") (:name "overview" :label "Overview" :field-type "textarea" :value ,(overview job) :required "required") (:label "Modify JOB" :field-type "button" :onclick "on_job_modify_submit_clicked()")) :link-fields `((:name "id" :field-type "hidden" :value ,(id job) :required "required") ,@(skill-checkboxes resume-pkg (id user) (id job)) (:label "Link Skills to Job" :field-type "button" :onclick "on_job_link_submit_clicked()")) :order-by "end_date desc, start_date desc, company asc")) (define-resume-component (:component-s contactinfo :rest-args ((address :parameter-type 'string) (city :parameter-type 'string) (state_id :parameter-type 'integer) (phone :parameter-type 'string) (email :parameter-type 'string) (url :parameter-type 'string) (visastatus_id :parameter-type 'integer)) :add-fields `((:name "address" :label "Address" :field-type "text" :required "required") (:name "city" :label "City" :field-type "text" :required "required") (:name "state_id" :label "State" :field-type "select" :options ,(state-options (get-records resume-pkg (make-instance 'states) "abbr asc")) :required "required") (:name "phone" :label "Phone" :field-type "text" :required "required") (:name "email" :label "Email" :field-type "text" :required "required") (:name "url" :label "URL" :field-type "text") (:name "visastatus_id" :label "VISA Status" :field-type "select" :options ,(visastatus-options (get-records resume-pkg (make-instance 'visastatus) "visastatus asc")) :required "required") (:label "Add CONTACTINFO" :field-type "button" :onclick "on_contactinfo_add_submit_clicked()")) :modify-fields `((:name "id" :field-type "hidden" :value ,(id contactinfo) :required "required") (:name "address" :label "Address" :field-type "text" :value ,(address contactinfo) :required "required") (:name "city" :label "City" :field-type "text" :value ,(city contactinfo) :required "required") (:name "state_id" :label "State" :field-type "select" :value ,(state_id contactinfo) :options ,(state-options (get-records resume-pkg (make-instance 'states) "abbr asc")) :required "required") (:name "phone" :label "Phone" :field-type "text" :value ,(phone contactinfo) :required "required") (:name "email" :label "Email" :field-type "text" :value ,(email contactinfo) :required "required") (:name "url" :label "URL" :field-type "text" :value ,(url contactinfo)) (:name "visastatus_id" :label "VISA Status" :field-type "select" :value (visastatus_id contactinfo) :options ,(visastatus-options (get-records resume-pkg (make-instance 'visastatus) "visastatus asc")) :required "required") (:label "Modify CONTACTINFO" :field-type "button" :onclick "on_contactinfo_modify_submit_clicked()")) :order-by "state_abbr asc, city asc, address asc" :view-p t)) (define-resume-component (:component-s education :rest-args ((school :parameter-type 'string) (major :parameter-type 'string) (minor :parameter-type 'string) (degree :parameter-type 'string) (progress :parameter-type 'string) (start_date :parameter-type 'string) (end_date :parameter-type 'string)) :add-fields `((:name "school" :label "School" :field-type "text" :required "required") (:name "major" :label "Major" :field-type "text" :required "required") (:name "minor" :label "Minor" :field-type "text") (:name "degree" :label "Degree" :field-type "text" :required "required") (:name "progress" :label "Progress" :field-type "text" :required "required") (:name "start_date" :label "Start Date" :field-type "text" :required "required") (:name "end_date" :label "End Date" :field-type "text" :required "required") (:label "Add EDUCATION" :field-type "button" :onclick "on_education_add_submit_clicked()")) :modify-fields `((:name "id" :field-type "hidden" :value ,(id education) :required "required") (:name "school" :label "School" :field-type "text" :value ,(school education) :required "required") (:name "major" :label "Major" :field-type "text" :value ,(major education) :required "required") (:name "minor" :label "Minor" :field-type "text" :value ,(minor education)) (:name "degree" :label "Degree" :field-type "text" :value ,(degree education) :required "required") (:name "progress" :label "Progress" :field-type "text" :value ,(progress education) :required "required") (:name "start_date" :label "Start Date" :field-type "text" :value ,(start_date education) :required "required") (:name "end_date" :label "End Date" :field-type "text" :value ,(end_date education) :required "required") (:label "Modify EDUCATION" :field-type "button" :onclick "on_education_modify_submit_clicked()")) :order-by "end_date desc, start_date desc, school asc, degree asc, major asc, minor asc")) (define-resume-component (:component-s language :rest-args ((name :parameter-type 'string) (understanding_listening :parameter-type 'string) (understanding_reading :parameter-type 'string) (speaking_interaction :parameter-type 'string) (speaking_production :parameter-type 'string) (writing :parameter-type 'string)) :add-fields `((:name "name" :label "Name" :field-type "text" :required "required") (:name "understanding_listening" :label "Understanding Listening" :field-type "select" :options ,(language-options) :required "required") (:name "understanding_reading" :label "Understanding Reading" :field-type "select" :options ,(language-options) :required "required") (:name "speaking_interaction" :label "Speaking Interaction" :field-type "select" :options ,(language-options) :required "required") (:name "speaking_production" :label "Speaking Production" :field-type "select" :options ,(language-options) :required "required") (:name "writing" :label "Writing" :field-type "select" :options ,(language-options) :required "required") (:label "Add LANGUAGE" :field-type "button" :onclick "on_language_add_submit_clicked()")) :modify-fields `((:name "id" :field-type "hidden" :value ,(id language) :required "required") (:name "name" :label "Name" :field-type "text" :value ,(name language) :required "required") (:name "understanding_listening" :label "Understanding Listening" :field-type "select" :options ,(language-options) :value ,(understanding_listening language) :required "required") (:name "understanding_reading" :label "Understanding Reading" :field-type "select" :options ,(language-options) :value ,(understanding_reading language) :required "required") (:name "speaking_interaction" :label "Speaking Interaction" :field-type "select" :options ,(language-options) :value ,(speaking_interaction language) :required "required") (:name "speaking_production" :label "Speaking Production" :field-type "select" :options ,(language-options) :value ,(speaking_production language) :required "required") (:name "writing" :label "Writing" :field-type "select" :options ,(language-options) :value ,(writing language) :required "required") (:label "Modify LANGUAGE" :field-type "button" :onclick "on_language_modify_submit_clicked()")) :order-by "name asc")) (define-resume-component (:component-s skill :rest-args ((skill :parameter-type 'string)) :add-fields `((:name "skill" :label "Skill" :field-type "text" :required "required") (:label "Add Skill" :field-type "button" :onclick "on_skill_add_submit_clicked()")) :modify-fields `((:name "id" :field-type "hidden" :value ,(id skill) :required "required") (:name "skill" :label "Skill" :field-type "text" :value ,(skill skill) :required "required") (:label "Modify SKILL" :field-type "button" :onclick "on_skill_modify_submit_clicked()")) :order-by "skill asc"))