<commit_before>{models, Resource, filters} = require '../resource_helper'

class CompetitionResource extends Resource
  resourceName: 'competitions'
  model: models.competition

  getEndpoints: -> [
    getByOwner
    index
    'show'
    'update'
    'destroy'
    'create'
    'patch'
  ]

#
# Custom endpoints defined here
#
getByOwner =
  route: '/by-owner/:ownerId'
  method: 'GET'
  filters: [filters.FromUrlParams, filters.FromQueryParams]
  emptyResult: []
  handler: ->
    ownerId = @params.ownerId
    showClosed = @params.showClosed
    query =
      owner_id: ownerId
    unless showClosed == 'true' or showClosed == true
      query.open = true
    console.log "[HTTP] request to list competitions by owner (ownerId=#{ownerId}, showClosed=#{showClosed})"
    @api.find(query)

index =
  route: '/'
  method: 'GET'
  filters: [filters.FromUrlParams, filters.FromQueryParams]
  emptyResult: []
  handler: ->
    showClosed = @params.showClosed
    query = {}
    unless showClosed == 'true' or showClosed == true
      query.open = true
    console.log "[HTTP] request to list competitions (showClosed=#{showClosed})"
    @api.find(query)

module.exports = new CompetitionResource<commit_msg>Added find competition by membership<commit_after>Q = require 'q'
{models, Resource, filters} = require '../resource_helper'

class CompetitionResource extends Resource
  resourceName: 'competitions'
  model: models.competition

  getEndpoints: -> [
    getByOwner
    index
    getByMembership
    end
    restart
    'show'
    'update'
    'destroy'
    'create'
    'patch'
  ]

#
# Custom endpoints defined here
#
getByOwner =
  route: '/by-owner/:ownerId'
  method: 'GET'
  filters: [filters.FromUrlParams, filters.FromQueryParams]
  handler: ->
    ownerId = @params.ownerId
    showClosed = @params.showClosed
    query =
      owner_id: ownerId
    unless showClosed == 'true' or showClosed == true
      query.open = true
    console.log "[HTTP] request to list competitions by owner (ownerId=#{ownerId}, showClosed=#{showClosed})"
    @api.list(query)

index =
  route: '/'
  method: 'GET'
  filters: [filters.FromUrlParams, filters.FromQueryParams]
  handler: ->
    showClosed = @params.showClosed
    query = {}
    unless showClosed == 'true' or showClosed == true
      query.open = true
    console.log "[HTTP] request to list competitions (showClosed=#{showClosed})"
    @api.list(query)

getByMembership =
  route: '/by-membership/:userId'
  method: 'GET'
  filters: [filters.FromUrlParams, filters.FromQueryParams]
  handler: ->
    userId = @params.userId
    showClosed = @params.showClosed == 'true' or @params.showClosed == true
    competitionsQuery = {}
    membershipApi = @createApi(@adapter, @models.competition_membership)
    console.log "[HTTP] request to list competitions by membership (userId=#{userId}, showClosed=#{showClosed})"
    membershipApi.list(user_id: userId)
    .then((memberships) =>
      if memberships?.length == 0
        console.log "[HTTP] found no competition memberships (userId=#{userId})"
        return []

      membershipIds = memberships.map((membership) ->
        _id: membership.competition_id
      )
      anyCompetition = { $or: membershipIds }
      dbQuery = @model.find(anyCompetition)
      unless showClosed
        dbQuery.where('open').equals(true)
      Q(dbQuery.exec())
    )

end =
  route: '/:id/end'
  filters: [filters.FromUrlParams]
  method: 'POST'
  handler: ->
    competitionId = @params.id
    console.log "[HTTP] end competition (competitionId=#{competitionId})"
    @api.update(
      { _id: competitionId },
      { open: false }
    )

restart =
  route: '/:id/restart'
  filters: [filters.FromUrlParams]
  method: 'POST'
  handler: ->
    competitionId = @params.id
    console.log "[HTTP] start competition (competitionId=#{competitionId})"
    @api.update(
      { _id: competitionId },
      { open: true }
    )

module.exports = new CompetitionResource
{'fraction_non_alphanumeric': 0.12515802781289506, 'fraction_numerical': 0.00025284450063211124, 'mean_word_length': 3.356828193832599, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '6513848', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1223, 'n_words': 289}