<commit_before>goog.provide 'app.Storage'

goog.require 'este.Storage'

class app.Storage extends este.Storage

  ###*
    @param {app.Dispatcher} dispatcher
    @param {app.LocalStorage} localStorage
    @param {app.Routes} routes
    @param {app.Xhr} xhr
    @param {app.songs.Store} songsStore
    @param {app.users.Store} usersStore
    @constructor
    @extends {este.Storage}
  ###
  constructor: (dispatcher, @localStorage, @routes, @xhr,
      @songsStore, @usersStore) ->

    dispatcher.register (action, payload) =>
      switch action
        when app.Actions.LOAD_ROUTE
          @loadRoute_ payload.route, payload.params
        when app.Actions.SEARCH_SONG
          # TODO: To method.
          @xhr
            .get @routes.api.songs.search.url null, query: payload.query
            .then (songs) =>
              @songsStore.fromJson foundSongs: songs

  init: ->
    @localStorage.sync [@usersStore]

  ###*
    @param {este.Route} route
    @param {Object} params
    @return {!goog.Promise}
    @private
  ###
  loadRoute_: (route, params) ->
    switch route
      when @routes.about, @routes.home, @routes.newSong, @routes.trash
        @ok()
      when @routes.me
        return @notFound() if !@usersStore.isLogged()
        @ok()
      when @routes.mySong, @routes.editSong
        return @notFound() if !@usersStore.songById params.id
        @ok()
      when @routes.song
        @xhr
          .get @routes.api.songs.byUrl.url params
          .then (songs) =>
            return @notFound() if !songs.length
            @songsStore.fromJson songsByUrl: songs
      when @routes.songs
        @ok()
      when @routes.recentlyUpdatedSongs
        @xhr
          .get @routes.api.songs.recentlyUpdated.url()
          .then (songs) =>
            @songsStore.fromJson recentlyUpdatedSongs: songs
      else
        @notFound()

#     when actions.PUBLISH_SONG
#       @xhr
#         .put @routes.api.songs.id.url(id: payload.song.id), payload.json
#         .then => @usersStore.setSongPublisher payload.song
#     when actions.UNPUBLISH_SONG
#       @xhr
#         .delete @routes.api.songs.id.url(id: payload.song.id)
#         .then => @usersStore.removeSongPublisher payload.song<commit_msg>Refactor SEARCH_SONG to method.<commit_after>goog.provide 'app.Storage'

goog.require 'este.Storage'

class app.Storage extends este.Storage

  ###*
    @param {app.Dispatcher} dispatcher
    @param {app.LocalStorage} localStorage
    @param {app.Routes} routes
    @param {app.Xhr} xhr
    @param {app.songs.Store} songsStore
    @param {app.users.Store} usersStore
    @constructor
    @extends {este.Storage}
  ###
  constructor: (dispatcher, @localStorage, @routes, @xhr,
      @songsStore, @usersStore) ->

    dispatcher.register (action, payload) =>
      switch action
        when app.Actions.LOAD_ROUTE
          @loadRoute_ payload.route, payload.params
        when app.Actions.SEARCH_SONG
          @searchSong_ payload.query

  init: ->
    @localStorage.sync [@usersStore]

  ###*
    @param {este.Route} route
    @param {Object} params
    @return {!goog.Promise}
    @private
  ###
  loadRoute_: (route, params) ->
    switch route
      when @routes.about, @routes.home, @routes.newSong, @routes.trash
        @ok()
      when @routes.me
        return @notFound() if !@usersStore.isLogged()
        @ok()
      when @routes.mySong, @routes.editSong
        return @notFound() if !@usersStore.songById params.id
        @ok()
      when @routes.song
        @xhr
          .get @routes.api.songs.byUrl.url params
          .then (songs) =>
            return @notFound() if !songs.length
            @songsStore.fromJson songsByUrl: songs
      when @routes.songs
        @ok()
      when @routes.recentlyUpdatedSongs
        @xhr
          .get @routes.api.songs.recentlyUpdated.url()
          .then (songs) =>
            @songsStore.fromJson recentlyUpdatedSongs: songs
      else
        @notFound()

  ###*
    @param {string} query
    @return {!goog.Promise}
    @private
  ###
  searchSong_: (query) ->
    @xhr
      .get @routes.api.songs.search.url null, query: query
      .then (songs) =>
        @songsStore.fromJson foundSongs: songs

#     when actions.PUBLISH_SONG
#       @xhr
#         .put @routes.api.songs.id.url(id: payload.song.id), payload.json
#         .then => @usersStore.setSongPublisher payload.song
#     when actions.UNPUBLISH_SONG
#       @xhr
#         .delete @routes.api.songs.id.url(id: payload.song.id)
#         .then => @usersStore.removeSongPublisher payload.song
{'fraction_non_alphanumeric': 0.1248626071664102, 'fraction_numerical': 0.0, 'mean_word_length': 2.330893118594436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '1180048', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1477, 'n_words': 309}