in app/services/editions/db/CollectionsQueries.scala [398:454]
protected def insertCollection(
frontId: String,
collectionIndex: Int,
name: String,
now: OffsetDateTime,
user: User
)(implicit session: DBSession): Either[Error, String] = {
logger.info(
s"Inserting new collection into front $frontId at index $collectionIndex"
)
for {
currentCollectionIds <- getCollectionIdsInFront(frontId)
maxCollectionIndex = currentCollectionIds.size
_ <-
if (collectionIndex > maxCollectionIndex) {
Left(
EditionsDB.InvalidInput(
s"Cannot add a collection at index $collectionIndex (min: 0, max: $maxCollectionIndex"
)
)
} else Right(())
// Make a gap in the index for the new collection
_ <- updateCollectionIndices(
currentCollectionIds.slice(collectionIndex, currentCollectionIds.size),
Some(collectionIndex + 1)
)
id <- Try {
sql"""
INSERT INTO collections (
front_id,
index,
name,
is_hidden,
updated_on,
updated_by,
updated_email
) VALUES (
$frontId
, $collectionIndex
, $name
, FALSE
, $now
, ${EditionsDB.getUserName(user)}
, ${user.email}
)
RETURNING id;
""".map(_.string("id"))
.single
.apply()
.toRight(
EditionsDB.WriteError("Could not write new collection to database")
)
}.toEither.left.map { error =>
EditionsDB.WriteError(error.getMessage)
}.flatten
} yield id
}