Commit 7d707ed0 authored by s8_vaitku_e's avatar s8_vaitku_e
Browse files

Initial commit

parents
Pipeline #1631 failed with stages
const http = require('http')
const { getLietotaji, getLietotajs, createLietotajs, updateLietotajs, deleteLietotajs } = require('./controllers/lietotajsController')
const app = http.createApp((req, res) => {
if(req.url === '/api/lietotaji' && req.method === 'GET') {
getlietotaji(req, res)
} else if(req.url.match(/\/api\/lietotaji\/\w+/) && req.method === 'GET') {
const id = req.url.split('/')[3]
getLietotajs(req, res, id)
} else if(req.url === '/api/lietotaji' && req.method === 'POST') {
createLietotajs(req, res)
} else if(req.url.match(/\/api\/lietotaji\/\w+/) && req.method === 'PUT') {
const id = req.url.split('/')[3]
updateLietotajs(req, res, id)
} else if(req.url.match(/\/api\/lietotaji\/\w+/) && req.method === 'DELETE') {
const id = req.url.split('/')[3]
deleteLietotajs(req, res, id)
} else {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ message: 'Route Not Found' }))
}
})
const PORT = process.env.PORT || 5000
app.listen(PORT, () => console.log(`App running on port ${PORT}`))
module.exports = app;
const Lietotajs = require('../models/lietotajsModel')
const { getPostData } = require('../utils')
// @desc Gets All Lietotaji
// @route GET /api/lietotaji
async function getLietotaji(req, res) {
try {
const lietotaji = await Lietotajs.findAll()
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(lietotaji))
} catch (error) {
console.log(error)
}
}
// @desc Gets Single Lietotajs
// @route GET /api/lietotajs/:id
async function getLietotajs(req, res, id) {
try {
const lietotajs = await Lietotajs.findById(id)
if(!lietotajs) {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ message: 'Lietotajs Not Found' }))
} else {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(lietotajs))
}
} catch (error) {
console.log(error)
}
}
// @desc Create a Lietotajs
// @route POST /api/lietotaji
async function createLietotajs(req, res) {
try {
const body = await getPostData(req)
const { name, surname, age } = JSON.parse(body)
const lietotajs = {
name,
surname,
age
}
const newLietotajs = await Lietotajs.create(lietotajs)
res.writeHead(201, { 'Content-Type': 'application/json' })
return res.end(JSON.stringify(newLietotajs))
} catch (error) {
console.log(error)
}
}
// @desc Update a Lietotajs
// @route PUT /api/lietotaji/:id
async function updateLietotajs(req, res, id) {
try {
const lietotajs = await Lietotajs.findById(id)
if(!lietotajs) {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ message: 'Lietotajs Not Found' }))
} else {
const body = await getPostData(req)
const { name, surname, age } = JSON.parse(body)
const lietotajsData = {
name: name || lietotajs.name,
surname: surname || lietotajs.surname,
age: age || lietotajs.age
}
const updLietotajs = await Lietotajs.update(id, lietotajsData)
res.writeHead(200, { 'Content-Type': 'application/json' })
return res.end(JSON.stringify(updLietotajs))
}
} catch (error) {
console.log(error)
}
}
// @desc Delete Lietotajs
// @route DELETE /api/lietotajs/:id
async function deleteLietotajs(req, res, id) {
try {
const lietotajs = await Lietotajs.findById(id)
if(!lietotajs) {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ message: 'Lietotajs Not Found' }))
} else {
await Lietotajs.remove(id)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ message: `Lietotajs ${id} removed` }))
}
} catch (error) {
console.log(error)
}
}
module.exports = {
getLietotaji,
getLietotajs,
createLietotajs,
updateLietotajs,
deleteLietotajs
}
\ No newline at end of file
[
{"id":"1","name":"Juris","surname":"Apse","age":26},
{"id":"2","name":"Anna","surname":"Klava","age":34},
{"id":"3","name":"Baiba","surname":"Jauka","age":31}
]
\ No newline at end of file
let lietotaji = require('../data/lietotaji')
//const { v4: uuidv4 } = require('uuid')
const { writeDataToFile } = require('../utils')
function findAll() {
return new Promise((resolve, reject) => {
resolve(lietotaji)
})
}
function findById(id) {
return new Promise((resolve, reject) => {
const lietotajs = lietotaji.find((l) => l.id === id)
resolve(lietotajs)
})
}
function create(lietotajs) {
return new Promise((resolve, reject) => {
const newLietotajs = {id: uuidv4(), ...lietotajs}
lietotaji.push(newLietotajs)
if (process.env.NODE_ENV !== 'test') {
writeDataToFile('./data/lietotaji.json', lietotaji);
}
resolve(newLietotajs)
})
}
function update(id, lietotajs) {
return new Promise((resolve, reject) => {
const index = lietotaji.findIndex((l) => l.id === id)
lietotaji[index] = {id, ...lietotajs}
if (process.env.NODE_ENV !== 'test') {
writeDataToFile('./data/lietotaji.json', lietotaji);
}
resolve(lietotaji[index])
})
}
function remove(id) {
return new Promise((resolve, reject) => {
lietotaji = lietotaji.filter((l) => l.id !== id)
if (process.env.NODE_ENV !== 'test') {
writeDataToFile('./data/lietotaji.json', lietotaji);
}
resolve()
})
}
module.exports = {
findAll,
findById,
create,
update,
remove
}
\ No newline at end of file
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../mime/cli.js" "$@"
ret=$?
else
node "$basedir/../mime/cli.js" "$@"
ret=$?
fi
exit $ret
@ECHO off
SETLOCAL
CALL :find_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
"%_prog%" "%dp0%\..\mime\cli.js" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
& "$basedir/node$exe" "$basedir/../mime/cli.js" $args
$ret=$LASTEXITCODE
} else {
& "node$exe" "$basedir/../mime/cli.js" $args
$ret=$LASTEXITCODE
}
exit $ret
1.3.7 / 2019-04-29
==================
* deps: negotiator@0.6.2
- Fix sorting charset, encoding, and language with extra parameters
1.3.6 / 2019-04-28
==================
* deps: mime-types@~2.1.24
- deps: mime-db@~1.40.0
1.3.5 / 2018-02-28
==================
* deps: mime-types@~2.1.18
- deps: mime-db@~1.33.0
1.3.4 / 2017-08-22
==================
* deps: mime-types@~2.1.16
- deps: mime-db@~1.29.0
1.3.3 / 2016-05-02
==================
* deps: mime-types@~2.1.11
- deps: mime-db@~1.23.0
* deps: negotiator@0.6.1
- perf: improve `Accept` parsing speed
- perf: improve `Accept-Charset` parsing speed
- perf: improve `Accept-Encoding` parsing speed
- perf: improve `Accept-Language` parsing speed
1.3.2 / 2016-03-08
==================
* deps: mime-types@~2.1.10
- Fix extension of `application/dash+xml`
- Update primary extension for `audio/mp4`
- deps: mime-db@~1.22.0
1.3.1 / 2016-01-19
==================
* deps: mime-types@~2.1.9
- deps: mime-db@~1.21.0
1.3.0 / 2015-09-29
==================
* deps: mime-types@~2.1.7
- deps: mime-db@~1.19.0
* deps: negotiator@0.6.0
- Fix including type extensions in parameters in `Accept` parsing
- Fix parsing `Accept` parameters with quoted equals
- Fix parsing `Accept` parameters with quoted semicolons
- Lazy-load modules from main entry point
- perf: delay type concatenation until needed
- perf: enable strict mode
- perf: hoist regular expressions
- perf: remove closures getting spec properties
- perf: remove a closure from media type parsing
- perf: remove property delete from media type parsing
1.2.13 / 2015-09-06
===================
* deps: mime-types@~2.1.6
- deps: mime-db@~1.18.0
1.2.12 / 2015-07-30
===================
* deps: mime-types@~2.1.4
- deps: mime-db@~1.16.0
1.2.11 / 2015-07-16
===================
* deps: mime-types@~2.1.3
- deps: mime-db@~1.15.0
1.2.10 / 2015-07-01
===================
* deps: mime-types@~2.1.2
- deps: mime-db@~1.14.0
1.2.9 / 2015-06-08
==================
* deps: mime-types@~2.1.1
- perf: fix deopt during mapping
1.2.8 / 2015-06-07
==================
* deps: mime-types@~2.1.0
- deps: mime-db@~1.13.0
* perf: avoid argument reassignment & argument slice
* perf: avoid negotiator recursive construction
* perf: enable strict mode
* perf: remove unnecessary bitwise operator
1.2.7 / 2015-05-10
==================
* deps: negotiator@0.5.3
- Fix media type parameter matching to be case-insensitive
1.2.6 / 2015-05-07
==================
* deps: mime-types@~2.0.11
- deps: mime-db@~1.9.1
* deps: negotiator@0.5.2
- Fix comparing media types with quoted values
- Fix splitting media types with quoted commas
1.2.5 / 2015-03-13
==================
* deps: mime-types@~2.0.10
- deps: mime-db@~1.8.0
1.2.4 / 2015-02-14
==================
* Support Node.js 0.6
* deps: mime-types@~2.0.9
- deps: mime-db@~1.7.0
* deps: negotiator@0.5.1
- Fix preference sorting to be stable for long acceptable lists
1.2.3 / 2015-01-31
==================
* deps: mime-types@~2.0.8
- deps: mime-db@~1.6.0
1.2.2 / 2014-12-30
==================
* deps: mime-types@~2.0.7
- deps: mime-db@~1.5.0
1.2.1 / 2014-12-30
==================
* deps: mime-types@~2.0.5
- deps: mime-db@~1.3.1
1.2.0 / 2014-12-19
==================
* deps: negotiator@0.5.0
- Fix list return order when large accepted list
- Fix missing identity encoding when q=0 exists
- Remove dynamic building of Negotiator class
1.1.4 / 2014-12-10
==================
* deps: mime-types@~2.0.4
- deps: mime-db@~1.3.0
1.1.3 / 2014-11-09
==================
* deps: mime-types@~2.0.3
- deps: mime-db@~1.2.0
1.1.2 / 2014-10-14
==================
* deps: negotiator@0.4.9
- Fix error when media type has invalid parameter
1.1.1 / 2014-09-28
==================
* deps: mime-types@~2.0.2
- deps: mime-db@~1.1.0
* deps: negotiator@0.4.8
- Fix all negotiations to be case-insensitive
- Stable sort preferences of same quality according to client order
1.1.0 / 2014-09-02
==================
* update `mime-types`
1.0.7 / 2014-07-04
==================
* Fix wrong type returned from `type` when match after unknown extension
1.0.6 / 2014-06-24
==================
* deps: negotiator@0.4.7
1.0.5 / 2014-06-20
==================
* fix crash when unknown extension given
1.0.4 / 2014-06-19
==================
* use `mime-types`
1.0.3 / 2014-06-11
==================
* deps: negotiator@0.4.6
- Order by specificity when quality is the same
1.0.2 / 2014-05-29
==================
* Fix interpretation when header not in request
* deps: pin negotiator@0.4.5
1.0.1 / 2014-01-18
==================
* Identity encoding isn't always acceptable
* deps: negotiator@~0.4.0
1.0.0 / 2013-12-27
==================
* Genesis
(The MIT License)
Copyright (c) 2014 Jonathan Ong <me@jongleberry.com>
Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# accepts
[![NPM Version][npm-version-image]][npm-url]
[![NPM Downloads][npm-downloads-image]][npm-url]
[![Node.js Version][node-version-image]][node-version-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator).
Extracted from [koa](https://www.npmjs.com/package/koa) for general use.
In addition to negotiator, it allows:
- Allows types as an array or arguments list, ie `(['text/html', 'application/json'])`
as well as `('text/html', 'application/json')`.
- Allows type shorthands such as `json`.
- Returns `false` when no types match
- Treats non-existent headers as `*`
## Installation
This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
```sh
$ npm install accepts
```
## API
<!-- eslint-disable no-unused-vars -->
```js
var accepts = require('accepts')
```
### accepts(req)
Create a new `Accepts` object for the given `req`.
#### .charset(charsets)
Return the first accepted charset. If nothing in `charsets` is accepted,
then `false` is returned.
#### .charsets()
Return the charsets that the request accepts, in the order of the client's
preference (most preferred first).
#### .encoding(encodings)
Return the first accepted encoding. If nothing in `encodings` is accepted,
then `false` is returned.
#### .encodings()
Return the encodings that the request accepts, in the order of the client's
preference (most preferred first).
#### .language(languages)
Return the first accepted language. If nothing in `languages` is accepted,
then `false` is returned.
#### .languages()
Return the languages that the request accepts, in the order of the client's
preference (most preferred first).
#### .type(types)
Return the first accepted type (and it is returned as the same text as what
appears in the `types` array). If nothing in `types` is accepted, then `false`
is returned.
The `types` array can contain full MIME types or file extensions. Any value
that is not a full MIME types is passed to `require('mime-types').lookup`.
#### .types()
Return the types that the request accepts, in the order of the client's
preference (most preferred first).
## Examples
### Simple type negotiation