<commit_before>/* -*- mode: javascript -*- */
import DB from './db'
import {hydra, schema, xhtml, POST, PUT, DELETE} from './namespaces'
import {Map, Set, List} from 'immutable'
import {Resource, Prefix, URI} from 'jsonld-dsl'
import {Left, Right} from 'fantasy-eithers'


/*************/
/* Resources */
/*************/
export const contextResource = Resource(
    Prefix('schema', 'http://schema.org/', schema)
  , Prefix('hydra', 'http://www.w3.org/ns/hydra/core#', hydra)
  , Prefix('xhtml', 'http://www.w3.org/1999/xhtml#', xhtml)
)

export const Index = {
  GET: (root, bookmarkModel) =>
    DB.all(bookmarkModel).then(
      x => Resource(
        contextLink(root),
        IndexResource(root, x.map(x => BookmarkResource(root, x)))
      )
    ),

  POST: (root, bookmarkModel, body) =>
    validateBookmarkForm(body).bimap(
      errors => Resource(
        contextLink(root),
        schema.error(errors),
        IndexResource(root)
      ),
      form => DB.post(
        bookmarkModel, form
      ).then(
        bookmark => Resource(
          contextLink(root),
          BookmarkResource(root, bookmark)
        )
      )
    )
}

export const Bookmark = {
  GET: (root, bookmarkModel, id) =>
    DB.get(
      bookmarkModel, id
      ).then(
        option => option.map(
          x => Resource(
            BookmarkResource(root, x)),
            contextLink(root)
          )
      ),
  PUT: (root, bookmarkModel, id, body) =>
     validateBookmarkForm(body)
      .bimap(
        errors => Resource(
          contextLink(root),
          schema.error(errors)
        ),
        form => DB.put(
          bookmarkModel, id, body
        ).then(
          option => option.map(x => null)
        )
      ),
  DELETE: (root, bookmarkModel, id) =>
    DB.delete(
      bookmarkModel, id
    ).then(
      option => option.map(x => null)
    )
}


const contextLink = root => Map(
  {'@context': root + '/context.json'}
)

const bookmarkForm = schema.WebPage(
  hydra.supportedProperty(
    [
      hydra.SupportedProperty(
        hydra.property('name'),
        hydra.required(true)
      ),
      
      hydra.SupportedProperty(
        hydra.property('url'),
        hydra.required(true)
      )
    ]
  )
)

const errorResource = description => Resource(
  schema.description(description)
)


// Returns a Left<Set<Resource>> if there are errors or Right(data) if valid
const validateBookmarkForm = data => {
  let nameErrors = !data.name ? Set.of(errorResource(".name undefined")) : Set()
  let urlErrors = !data.url ? Set.of(errorResource(".url undefined")) : Set()
  let errors = nameErrors.union(urlErrors)
  if(!errors.isEmpty()) {
    return Left(errors)
  } else {
    return Right(data)
  }
}

const BookmarkResource = (root, bookmark) => schema.WebPage(
    URI(root + '/' + bookmark.id)
  , PUT(hydra.expects(bookmarkForm), hydra.statusCodes([201, 400]))
  , DELETE(hydra.statusCodes([201]))
  , xhtml.up(URI(root + '/'))
  , bookmark
)

const IndexResource = (root, members) => hydra.Collection(
    URI(root + '/')
  , POST(hydra.expects(bookmarkForm), hydra.statusCodes([303, 400]))
  , hydra.member(List(members))
)


<commit_msg>removed the silly prefix commas<commit_after>/* -*- mode: javascript -*- */
import DB from './db'
import {hydra, schema, xhtml, POST, PUT, DELETE} from './namespaces'
import {Map, Set, List} from 'immutable'
import {Resource, Prefix, URI} from 'jsonld-dsl'
import {Left, Right} from 'fantasy-eithers'


/*************/
/* Resources */
/*************/
export const contextResource = Resource(
  Prefix('schema', 'http://schema.org/', schema),
  Prefix('hydra', 'http://www.w3.org/ns/hydra/core#', hydra),
  Prefix('xhtml', 'http://www.w3.org/1999/xhtml#', xhtml)
)

export const Index = {
  GET: (root, bookmarkModel) =>
    DB.all(bookmarkModel).then(
      x => Resource(
        contextLink(root),
        IndexResource(root, x.map(x => BookmarkResource(root, x)))
      )
    ),

  POST: (root, bookmarkModel, body) =>
    validateBookmarkForm(body).bimap(
      errors => Resource(
        contextLink(root),
        schema.error(errors),
        IndexResource(root)
      ),
      form => DB.post(
        bookmarkModel, form
      ).then(
        bookmark => Resource(
          contextLink(root),
          BookmarkResource(root, bookmark)
        )
      )
    )
}

export const Bookmark = {
  GET: (root, bookmarkModel, id) =>
    DB.get(
      bookmarkModel, id
      ).then(
        option => option.map(
          x => Resource(
            BookmarkResource(root, x)),
            contextLink(root)
          )
      ),
  PUT: (root, bookmarkModel, id, body) =>
     validateBookmarkForm(body)
      .bimap(
        errors => Resource(
          contextLink(root),
          schema.error(errors)
        ),
        form => DB.put(
          bookmarkModel, id, body
        ).then(
          option => option.map(x => null)
        )
      ),
  DELETE: (root, bookmarkModel, id) =>
    DB.delete(
      bookmarkModel, id
    ).then(
      option => option.map(x => null)
    )
}


const contextLink = root => Map(
  {'@context': root + '/context.json'}
)

const bookmarkForm = schema.WebPage(
  hydra.supportedProperty(
    [
      hydra.SupportedProperty(
        hydra.property('name'),
        hydra.required(true)
      ),
      
      hydra.SupportedProperty(
        hydra.property('url'),
        hydra.required(true)
      )
    ]
  )
)

const errorResource = description => Resource(
  schema.description(description)
)


// Returns a Left<Set<Resource>> if there are errors or Right(data) if valid
const validateBookmarkForm = data => {
  let nameErrors = !data.name ? Set.of(errorResource(".name undefined")) : Set()
  let urlErrors = !data.url ? Set.of(errorResource(".url undefined")) : Set()
  let errors = nameErrors.union(urlErrors)
  if(!errors.isEmpty()) {
    return Left(errors)
  } else {
    return Right(data)
  }
}

const BookmarkResource = (root, bookmark) => schema.WebPage(
  URI(root + '/' + bookmark.id),
  PUT(hydra.expects(bookmarkForm), hydra.statusCodes([201, 400])),
  DELETE(hydra.statusCodes([201])),
  xhtml.up(URI(root + '/')),
  bookmark
)

const IndexResource = (root, members) => hydra.Collection(
  URI(root + '/'),
  POST(hydra.expects(bookmarkForm), hydra.statusCodes([303, 400])),
  hydra.member(List(members))
)



{'fraction_non_alphanumeric': 0.16093404859577154, 'fraction_numerical': 0.006626696118649416, 'mean_word_length': 2.611965811965812, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5855377', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2033, 'n_words': 424}