<commit_before>(ns open-company.representations.section
  (:require [defun.core :refer (defun-)]
            [cheshire.core :as json]
            [open-company.representations.common :as common]
            [open-company.resources.section :as section]))

(def media-type "application/vnd.open-company.section.v1+json")
(def collection-media-type "application/vnd.collection+vnd.open-company.section+json;version=1")

(def ^:private clean-properties [:id :company-slug :section-name])

(defn url
  ([company-slug section-name]
  (str "/companies/" (name company-slug) "/" (name section-name)))
  ([company-slug section-name updated-at]
  (str (url company-slug section-name) "?as-of=" updated-at)))

(defn- revisions-url [company-slug section-name]
  (str (url company-slug section-name) "/revisions"))

(defn- self-link [company-slug section-name]
  (common/self-link (url company-slug section-name) media-type))

(defn- update-link [company-slug section-name]
  (common/update-link (url company-slug section-name) media-type))

(defn- partial-update-link [company-slug section-name]
  (common/partial-update-link (url company-slug section-name) media-type))

(defn- revisions-link [company-slug section-name]
  (common/link-map "revisions" common/GET (revisions-url company-slug section-name) media-type))

(defn- revision-link [company-slug section-name updated-at]
  (common/revision-link (url company-slug section-name updated-at) updated-at media-type))

(defun- section-links
  "Add the HATEAOS links to the section"
  ([section authorized] (section-links (:company-slug section) (:section-name section) section authorized))

  ; read/only links
  ([company-slug section-name section false]
  (assoc section :links [(self-link company-slug section-name)
                         (revisions-link company-slug section-name)]))

  ; read/write links
  ([company-slug section-name section true]
  (assoc section :links (flatten [
    (self-link company-slug section-name)
    (update-link company-slug section-name)]))))

(defn section-for-rendering
  "Get a representation of the section for the REST API"
  [conn {:keys [company-slug section-name] :as section} authorized]
  (-> section
    (assoc :revisions (section/list-revisions conn company-slug section-name))
    (update :revisions #(map (fn [rev] (revision-link company-slug section-name (:updated-at rev))) %))
    (section-links authorized)
    (common/clean clean-properties)))

(defn render-section
  "Create a JSON representation of the section for the REST API"
  ([conn section]<commit_msg>Missing revisions link addition in r/w links.<commit_after>(ns open-company.representations.section
  (:require [defun.core :refer (defun-)]
            [cheshire.core :as json]
            [open-company.representations.common :as common]
            [open-company.resources.section :as section]))

(def media-type "application/vnd.open-company.section.v1+json")
(def collection-media-type "application/vnd.collection+vnd.open-company.section+json;version=1")

(def ^:private clean-properties [:id :company-slug :section-name])

(defn url
  ([company-slug section-name]
  (str "/companies/" (name company-slug) "/" (name section-name)))
  ([company-slug section-name updated-at]
  (str (url company-slug section-name) "?as-of=" updated-at)))

(defn- revisions-url [company-slug section-name]
  (str (url company-slug section-name) "/revisions"))

(defn- self-link [company-slug section-name]
  (common/self-link (url company-slug section-name) media-type))

(defn- update-link [company-slug section-name]
  (common/update-link (url company-slug section-name) media-type))

(defn- partial-update-link [company-slug section-name]
  (common/partial-update-link (url company-slug section-name) media-type))

(defn- revisions-link [company-slug section-name]
  (common/link-map "revisions" common/GET (revisions-url company-slug section-name) media-type))

(defn- revision-link [company-slug section-name updated-at]
  (common/revision-link (url company-slug section-name updated-at) updated-at media-type))

(defun- section-links
  "Add the HATEAOS links to the section"
  ([section authorized] (section-links (:company-slug section) (:section-name section) section authorized))

  ; read/only links
  ([company-slug section-name section false]
  (assoc section :links [(self-link company-slug section-name)
                         (revisions-link company-slug section-name)]))

  ; read/write links
  ([company-slug section-name section true]
  (assoc section :links (flatten [
    (self-link company-slug section-name)
    (revisions-link company-slug section-name)
    (update-link company-slug section-name)]))))

(defn section-for-rendering
  "Get a representation of the section for the REST API"
  [conn {:keys [company-slug section-name] :as section} authorized]
  (-> section
    (assoc :revisions (section/list-revisions conn company-slug section-name))
    (update :revisions #(map (fn [rev] (revision-link company-slug section-name (:updated-at rev))) %))
    (section-links authorized)
    (common/clean clean-properties)))

(defn render-section
  "Create a JSON representation of the section for the REST API"
  ([conn section]
{'fraction_non_alphanumeric': 0.13873388493361555, 'fraction_numerical': 0.0007696748123917645, 'mean_word_length': 4.081133919843597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6710305', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1642, 'n_words': 426}