소스 검색

feat: file upload

set header and header row for uploading csv
master
Christof Nolle 2 년 전
부모
커밋
73f2a0841c

+ 0
- 7
README.md 파일 보기

@@ -49,10 +49,3 @@ Preise sind mit einer oder zwei Nachkommastellen und "." als Trenner zwischen Eu
- lines bezieht sich auf die Anzahl der zugrundeliegenden Artikel
- Response Code: 200 wenn alle Einträge erfolgreich verarbeitet werden konnten

### Test Service
- Download link: https://cloud.inoio.de/s/SnLlMwp4XEWlDe9/download
- Starten: `java -jar coding-challenge.jar`

### Entwicklungsumgebung

Schreibe den Code in der Sprache in der du dich wohl fühlst und so, dass du ihn für "production ready" hällst.

+ 10
- 8
src/main/scala/de/codingchallenge/repositories/ProductExportRepository.scala 파일 보기

@@ -1,29 +1,30 @@
package de.codingchallenge.repositories

import akka.NotUsed
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpEntity.Chunked
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.ContentNegotiator.Alternative.ContentType
import akka.stream.scaladsl.{Flow, JavaFlowSupport, Source}
import akka.stream.scaladsl.{Flow, Source}
import akka.util.ByteString
import de.codingchallenge.models.ProductExport
import de.codingchallenge.csv.CsvOps._
import de.codingchallenge.models.ProductExport

import scala.concurrent.Future

class ProductExportRepository(actorSystem: ActorSystem) {

// val headerLine = "produktId|name|beschreibung|preis|summeBestand"
val headerLine = "produktId|name|beschreibung|preis|summeBestand\n"

val baseUrl = "http://localhost:8080"

def add(p: Source[ProductExport, _], articlesSize: Int): Future[HttpResponse] = {

// it did not work with charset information
val entity = Chunked.fromData(
ContentTypes.`text/csv(UTF-8)`,
p.via(csvFlow))
ContentType.WithMissingCharset(MediaTypes.`text/csv`),
p.via(csvFlow)
.prepend(Source.single(ByteString(headerLine)))
)

Http()(actorSystem)
.singleRequest(
@@ -34,5 +35,6 @@ class ProductExportRepository(actorSystem: ActorSystem) {
}

private val csvFlow: Flow[ProductExport, ByteString, Any] =
Flow.fromFunction(p => ByteString(p.toCsvLine + "\n"))
Flow.fromFunction(p => ByteString(p.toCsvLine))

}

+ 4
- 0
src/test/scala/de/codingchallenge/repositories/ArticleRepositorySpec.scala 파일 보기

@@ -8,6 +8,10 @@ import org.scalatest.{BeforeAndAfterAll, MustMatchers, WordSpec}
import scala.concurrent.duration._
import scala.concurrent.Await

/**
* Honestly this is kind of an integration tests running against the Demo Server.
* I will leave it here for convenience
*/
class ArticleRepositorySpec extends BaseSpec with AkkaSpec {

"The ArticleRepository" must {

+ 24
- 0
src/test/scala/de/codingchallenge/repositories/ProductExportRepositorySpec.scala 파일 보기

@@ -0,0 +1,24 @@
package de.codingchallenge.repositories

import akka.http.scaladsl.model.StatusCodes
import akka.stream.scaladsl.{Sink, Source}
import de.codingchallenge.fixtures.ProductExportFixture
import de.codingchallenge.models.Article
import de.codingchallenge.{AkkaSpec, BaseSpec}

import scala.concurrent.Await
import scala.concurrent.duration._

/**
* Honestly this is kind of an integration tests running against the Demo Server
*/
class ProductExportRepositorySpec extends BaseSpec with AkkaSpec with ProductExportFixture {

"The ProductExportRepository" must {
"put product reports as expected" in {
val repo = new ProductExportRepository(system)
Await.result(repo.add(Source.single(productExport), 1), 2.second).status mustBe StatusCodes.OK
}
}

}

Loading…
취소
저장