Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
stocks-api-v2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sharma072072
stocks-api-v2
Commits
c68a9652
Commit
c68a9652
authored
May 07, 2022
by
sharma072072
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
end point created with repository test case
parent
d019eb8f
Pipeline
#2291
failed with stages
in 1 minute and 15 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
245 additions
and
0 deletions
+245
-0
Stocks.java
src/main/java/com/altimetrik/ee/demo/bean/Stocks.java
+110
-0
StocksController.java
...a/com/altimetrik/ee/demo/controller/StocksController.java
+43
-0
StocksRepository.java
...a/com/altimetrik/ee/demo/repository/StocksRepository.java
+13
-0
StocksService.java
...in/java/com/altimetrik/ee/demo/service/StocksService.java
+22
-0
project1170.mv.db
src/main/resources/project1170.mv.db
+0
-0
StocksJPATest.java
...test/java/com/altimetrik/ee/demo/tests/StocksJPATest.java
+57
-0
No files found.
src/main/java/com/altimetrik/ee/demo/bean/Stocks.java
0 → 100644
View file @
c68a9652
package
com
.
altimetrik
.
ee
.
demo
.
bean
;
import
java.util.Date
;
import
java.util.Objects
;
import
javax.persistence.*
;
@Entity
@Table
(
name
=
"stocks"
)
public
class
Stocks
{
@Id
@Column
(
name
=
"stockQuote"
)
String
stockQuote
;
@Column
(
name
=
"companyName"
)
String
companyName
;
@Column
(
name
=
"date"
)
Date
date
;
@Column
(
name
=
"price"
)
Double
price
;
@Column
(
name
=
"currency"
)
String
currency
;
public
Stocks
(
String
stockQuote
,
String
companyName
,
Date
date
,
Double
price
,
String
currency
)
{
this
.
stockQuote
=
stockQuote
;
this
.
companyName
=
companyName
;
this
.
date
=
date
;
this
.
price
=
price
;
this
.
currency
=
currency
;
}
public
Stocks
()
{
}
public
String
getStockQuote
()
{
return
stockQuote
;
}
public
String
getCompanyName
()
{
return
companyName
;
}
public
Date
getDate
()
{
return
date
;
}
public
Double
getPrice
()
{
return
price
;
}
public
String
getCurrency
()
{
return
currency
;
}
public
void
setStockQuote
(
String
stockQuote
)
{
this
.
stockQuote
=
stockQuote
;
}
public
void
setCompanyName
(
String
companyName
)
{
this
.
companyName
=
companyName
;
}
public
void
setDate
(
Date
date
)
{
this
.
date
=
date
;
}
public
void
setPrice
(
Double
price
)
{
this
.
price
=
price
;
}
public
void
setCurrency
(
String
currency
)
{
this
.
currency
=
currency
;
}
@Override
public
String
toString
()
{
return
"Stocks{"
+
"stockQuote='"
+
stockQuote
+
'\''
+
", companyName='"
+
companyName
+
'\''
+
", date="
+
date
+
", price="
+
price
+
", currency='"
+
currency
+
'\''
+
'}'
;
}
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
{
return
true
;
}
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
{
return
false
;
}
Stocks
stocks
=
(
Stocks
)
o
;
return
Objects
.
equals
(
stockQuote
,
stocks
.
stockQuote
)
&&
Objects
.
equals
(
companyName
,
stocks
.
companyName
)
&&
Objects
.
equals
(
date
,
stocks
.
date
)
&&
Objects
.
equals
(
price
,
stocks
.
price
)
&&
Objects
.
equals
(
currency
,
stocks
.
currency
);
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
stockQuote
,
companyName
,
date
,
price
,
currency
);
}
}
src/main/java/com/altimetrik/ee/demo/controller/StocksController.java
0 → 100644
View file @
c68a9652
package
com
.
altimetrik
.
ee
.
demo
.
controller
;
import
com.altimetrik.ee.demo.bean.Stocks
;
import
com.altimetrik.ee.demo.service.StocksService
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
@RequestMapping
(
"/stocks"
)
public
class
StocksController
{
@Autowired
StocksService
stocksService
;
@GetMapping
(
value
=
"/stockQuote"
,
produces
=
"application/json"
)
public
ResponseEntity
<
List
<
Stocks
>>
getSpecificStocks
(
@RequestParam
(
value
=
"symbols"
,
required
=
false
)
List
<
String
>
symbols
)
{
try
{
List
<
Stocks
>
stocksList
=
new
ArrayList
<
Stocks
>();
if
(
symbols
==
null
)
stocksService
.
getStocks
().
forEach
(
stocksList:
:
add
);
else
if
(
symbols
.
size
()<=
10
)
{
stocksService
.
getStocksByIds
(
symbols
).
forEach
(
stocksList:
:
add
);
}
else
return
new
ResponseEntity
<>(
HttpStatus
.
PAYLOAD_TOO_LARGE
);
if
(
stocksList
.
isEmpty
())
{
return
new
ResponseEntity
<>(
HttpStatus
.
NO_CONTENT
);
}
return
new
ResponseEntity
<>(
stocksList
,
HttpStatus
.
OK
);
}
catch
(
Exception
e
)
{
return
new
ResponseEntity
<>(
null
,
HttpStatus
.
INTERNAL_SERVER_ERROR
);
}
}
}
src/main/java/com/altimetrik/ee/demo/repository/StocksRepository.java
0 → 100644
View file @
c68a9652
package
com
.
altimetrik
.
ee
.
demo
.
repository
;
import
com.altimetrik.ee.demo.bean.Stocks
;
import
java.util.List
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.Query
;
public
interface
StocksRepository
extends
JpaRepository
<
Stocks
,
String
>
{
String
QUERY
=
"SELECT * FROM stocks WHERE stockQuote IN (?1)"
;
@Query
(
value
=
QUERY
,
nativeQuery
=
true
)
List
<
Stocks
>
findByStockQuotes
(
List
<
String
>
ids
);
}
src/main/java/com/altimetrik/ee/demo/service/StocksService.java
0 → 100644
View file @
c68a9652
package
com
.
altimetrik
.
ee
.
demo
.
service
;
import
com.altimetrik.ee.demo.bean.Stocks
;
import
com.altimetrik.ee.demo.repository.StocksRepository
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
@Service
public
class
StocksService
{
@Autowired
private
StocksRepository
repository
;
public
List
<
Stocks
>
getStocksByIds
(
List
<
String
>
ids
)
{
return
repository
.
findByStockQuotes
(
ids
);
}
public
List
<
Stocks
>
getStocks
()
{
return
repository
.
findAll
();
}
}
src/main/resources/project1170.mv.db
0 → 100644
View file @
c68a9652
File added
src/test/java/com/altimetrik/ee/demo/tests/StocksJPATest.java
0 → 100644
View file @
c68a9652
package
com
.
altimetrik
.
ee
.
demo
.
tests
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
com.altimetrik.ee.demo.bean.Stocks
;
import
com.altimetrik.ee.demo.repository.StocksRepository
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.List
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@DataJpaTest
public
class
StocksJPATest
{
@Autowired
private
StocksRepository
stocksRepository
;
@Test
public
void
shouldCreateNewStock
()
throws
ParseException
{
String
sDate1
=
"31/12/1998"
;
Date
date1
=
new
SimpleDateFormat
(
"dd/MM/yyyy"
).
parse
(
sDate1
);
Stocks
s1
=
new
Stocks
(
"HCL-1"
,
"HCL"
,
date1
,
420.0
,
"INR"
);
stocksRepository
.
save
(
s1
);
}
@Test
public
void
shouldGetStocksByName
()
throws
ParseException
{
String
sDate1
=
"31/12/1998"
;
Date
date1
=
new
SimpleDateFormat
(
"dd/MM/yyyy"
).
parse
(
sDate1
);
Stocks
s1
=
new
Stocks
(
"HCL-1"
,
"HCL"
,
date1
,
420.0
,
"INR"
);
Stocks
s2
=
new
Stocks
(
"HCL-2"
,
"HCL"
,
date1
,
520.0
,
"USD"
);
Stocks
s3
=
new
Stocks
(
"HCL-3"
,
"HCL"
,
date1
,
620.0
,
"JNY"
);
Stocks
s4
=
new
Stocks
(
"HCL-4"
,
"HCL"
,
date1
,
720.0
,
"INR"
);
List
<
Stocks
>
list
=
new
ArrayList
<>();
list
.
add
(
s1
);
list
.
add
(
s2
);
list
.
add
(
s3
);
list
.
add
(
s4
);
stocksRepository
.
saveAll
(
list
);
List
<
String
>
queries
=
new
ArrayList
<>();
queries
.
add
(
"HCL-1"
);
queries
.
add
(
"HCL-2"
);
queries
.
add
(
"HCL-3"
);
Iterable
<
Stocks
>
users
=
stocksRepository
.
findByStockQuotes
(
queries
);
users
.
forEach
(
e
->
System
.
out
.
println
(
e
.
toString
()));
assertThat
(
users
).
hasSize
(
3
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment