<commit_before>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 <NAME> <<EMAIL>>

(ns uxbox.services.projects
  (:require [mount.core :as mount :refer (defstate)]
            [suricatta.core :as sc]
            [buddy.hashers :as hashers]
            [buddy.sign.jwe :as jwe]
            [buddy.core.nonce :as nonce]
            [buddy.core.hash :as hash]
            [buddy.core.codecs :as codecs]
            [uxbox.config :as ucfg]
            [uxbox.schema :as us]
            [uxbox.persistence :as up]
            [uxbox.services.core :as usc]
            [uxbox.services.auth :as usauth]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Schema
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def ^:const +project-schema+
  {:id [us/required us/uuid]
   :user [us/required us/uuid]
   :name [us/required us/string]
   :width [us/required us/integer]
   :height [us/required us/integer]
   :layout [us/required us/string]})

(def ^:const +page-schema+
  (assoc +project-schema+
         :project [us/required us/uuid]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Repository
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; NOTE: maybe move sql into files?

(defn create-project
  [conn {:keys [id user name width height layout] :as data}]
  {:pre [(us/validate! data +project-schema+)]}
  (let [sql (str "INSERT INTO projects (id, user, name, width, height, layout)"
                 " VALUES (?, ?, ?, ?, ?) RETURNING *")
        sqlv [sql id user name width height layout]]
    (some-> (sc/fetch-one conn sqlv)
            (usc/normalize-attrs))))

(defn create-page
  [conn {:keys [id user project name width height layout] :as data}]
  {:pre [(us/validate! data +page-schema+)]}
  (let [sql (str "INSERT INTO pages (id, user, project, name, width, "
                 "                   height, layout)"
                 " VALUES (?, ?, ?, ?, ?, ?) RETURNING *")
        sqlv [sql id user project name width height layout]]
    (some-> (sc/fetch-one conn sqlv)
            (usc/normalize-attrs))))

(defn update-project
  [conn {:keys [id user name width height layout] :as data}]
  {:pre [(us/validate! data +project-schema+)]}
  (let [sql (str "UPDATE projects SET "
                 " name=?, width=? height=?, layout=?,"
                 " modified_at=current_timestamp "
                 " WHERE id=? AND user=? RETURNING *")
        sqlv [sql name width height layout id user]]
    (some-> (sc/fetch-one conn sqlv)
            (usc/normalize-attrs))))

(defn update-page
  [conn {:keys [id user project name width height layout] :as data}]
  {:pre [(us/validate! data +page-schema+)]}
  (let [sql (str "UPDATE projects SET "
                 " name=?, width=? height=?, layout=?,"
                 " modified_at=current_timestamp"
                 " WHERE id=? AND user=? AND project=?"
                 " RETURNING *")<commit_msg>Adapt project services to schema changes.<commit_after>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 <NAME> <<EMAIL>>

(ns uxbox.services.projects
  (:require [suricatta.core :as sc]
            [buddy.hashers :as hashers]
            [buddy.sign.jwe :as jwe]
            [buddy.core.nonce :as nonce]
            [buddy.core.hash :as hash]
            [buddy.core.codecs :as codecs]
            [uxbox.config :as ucfg]
            [uxbox.schema :as us]
            [uxbox.persistence :as up]
            [uxbox.services.core :as usc]
            [uxbox.services.auth :as usauth]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Schema
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def +project-schema+
  {:id [us/required us/uuid]
   :user [us/required us/uuid]
   :name [us/required us/string]})

(def +page-schema+
  {:id [us/required us/uuid]
   :user [us/required us/uuid]
   :project [us/required us/uuid]
   :name [us/required us/string]
   :width [us/required us/integer]
   :height [us/required us/integer]
   :layout [us/required us/string]})

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Repository
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; NOTE: maybe move sql into files?

(defn create-project
  [conn {:keys [id user name] :as data}]
  {:pre [(us/validate! data +project-schema+)]}
  (let [sql (str "INSERT INTO projects (id, \"user\", name)"
                 " VALUES (?, ?, ?) RETURNING *")
        sqlv [sql id user name]]
    (some-> (sc/fetch-one conn sqlv)
            (usc/normalize-attrs))))

(defn create-page
  [conn {:keys [id user project name width height layout] :as data}]
  {:pre [(us/validate! data +page-schema+)]}
  (let [sql (str "INSERT INTO pages (id, \"user\", project, name, width, "
                 "                   height, layout)"
                 " VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING *")
        sqlv [sql id user project name width height layout]]
    (some-> (sc/fetch-one conn sqlv)
            (usc/normalize-attrs))))

(defn update-project
  [conn {:keys [id user name width height layout] :as data}]
  {:pre [(us/validate! data +project-schema+)]}
  (let [sql (str "UPDATE projects SET "
                 " name=?, width=? height=?, layout=?,"
                 " modified_at=current_timestamp "
                 " WHERE id=? AND user=? RETURNING *")
        sqlv [sql name width height layout id user]]
    (some-> (sc/fetch-one conn sqlv)
            (usc/normalize-attrs))))

(defn update-page
  [conn {:keys [id user project name width height layout] :as data}]
  {:pre [(us/validate! data +page-schema+)]}
  (let [sql (str "UPDATE projects SET "
                 " name=?, width=? height=?, layout=?,"
                 " modified_at=current_timestamp"
                 " WHERE id=? AND user=? AND project=?"
                 " RETURNING *")
{'fraction_non_alphanumeric': 0.228393085787452, 'fraction_numerical': 0.002560819462227913, 'mean_word_length': 2.6608084358523727, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1859778', 'n_tokens_mistral': 2148, 'n_tokens_neox': 1962, 'n_words': 570}