<commit_before>/*
 * Part of GDL book_api.
 * Copyright (C) 2017 Global Digital Library
 *
 * See LICENSE
 */

package no.gdl.bookapi.repository

import com.typesafe.scalalogging.LazyLogging
import no.gdl.bookapi.model.api.OptimisticLockException
import no.gdl.bookapi.model.domain.Feed
import scalikejdbc._

trait FeedRepository {

  val feedRepository: FeedRepository

  class FeedRepository extends LazyLogging {
    private val f = Feed.syntax

    def forUrl(url: String)(implicit session: DBSession = ReadOnlyAutoSession): Option[Feed] = {
      select
        .from(Feed as f)
        .where.eq(f.url, url).toSQL
        .map(Feed(f)).single().apply()
    }

    // TODO Change this to "upsert" (on conflict do update) when we're on PostgreSQL >= 9.5
    // https://www.postgresql.org/docs/devel/static/sql-insert.html
    def addOrUpdate(feed: Feed)(implicit session: DBSession = AutoSession): Feed = {
      val f = Feed.column
      val startRevision = 1

      forUrl(feed.url) match {
        case Some(existingFeed) =>
          val newRevision = existingFeed.revision.getOrElse(0) + 1
          val count = update(Feed).set(
            f.revision -> newRevision,
            f.title -> feed.title,
            f.description -> feed.description
          ).where
            .eq(f.id, existingFeed.id).and
            .eq(f.revision, existingFeed.revision).toSQL.update().apply()<commit_msg>#94: This can be done now GlobalDigitalLibraryio/issues#94<commit_after>/*
 * Part of GDL book_api.
 * Copyright (C) 2017 Global Digital Library
 *
 * See LICENSE
 */

package no.gdl.bookapi.repository

import com.typesafe.scalalogging.LazyLogging
import no.gdl.bookapi.model.api.OptimisticLockException
import no.gdl.bookapi.model.domain.Feed
import scalikejdbc._

trait FeedRepository {

  val feedRepository: FeedRepository

  class FeedRepository extends LazyLogging {
    private val f = Feed.syntax

    def forUrl(url: String)(implicit session: DBSession = ReadOnlyAutoSession): Option[Feed] = {
      select
        .from(Feed as f)
        .where.eq(f.url, url).toSQL
        .map(Feed(f)).single().apply()
    }

    // TODO Change this to "upsert" (on conflict do update), which exists in PostgreSQL >= 9.5
    // https://www.postgresql.org/docs/devel/static/sql-insert.html
    def addOrUpdate(feed: Feed)(implicit session: DBSession = AutoSession): Feed = {
      val f = Feed.column
      val startRevision = 1

      forUrl(feed.url) match {
        case Some(existingFeed) =>
          val newRevision = existingFeed.revision.getOrElse(0) + 1
          val count = update(Feed).set(
            f.revision -> newRevision,
            f.title -> feed.title,
            f.description -> feed.description
          ).where
            .eq(f.id, existingFeed.id).and
            .eq(f.revision, existingFeed.revision).toSQL.update().apply()
{'fraction_non_alphanumeric': 0.11336599020293912, 'fraction_numerical': 0.007697690692792162, 'mean_word_length': 2.7033678756476682, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '373105', 'n_tokens_mistral': 921, 'n_tokens_neox': 868, 'n_words': 205}