Skip to content

Commit

Permalink
Merge pull request #32 from CMU-17-356/user
Browse files Browse the repository at this point in the history
created clicks property for user endpoints
  • Loading branch information
yinghork authored May 3, 2023
2 parents 9a64f66 + 4e800fd commit 9bff10b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 3 deletions.
5 changes: 5 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fly.toml
Dockerfile
.dockerignore
node_modules
.git
44 changes: 44 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=18.13.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="NodeJS"

# NodeJS app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV=production


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y python-is-python3 pkg-config build-essential

# Install node modules
COPY --link package.json package-lock.json .
RUN npm install --production=false

# Copy application code
COPY --link . .

# Build application
RUN npm run build

# Remove development dependencies
RUN npm prune --production


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
CMD [ "npm", "run", "start" ]
44 changes: 44 additions & 0 deletions backend/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# fly.toml app configuration file generated for doneserver on 2023-05-03T18:12:02-04:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "doneserver"
kill_signal = "SIGINT"
kill_timeout = 5
primary_region = "iad"
processes = []

[build]

[env]
PORT = "8080"

[experimental]
auto_rollback = true

[[services]]
http_checks = []
internal_port = 8080
processes = ["app"]
protocol = "tcp"
script_checks = []
[services.concurrency]
hard_limit = 25
soft_limit = 20
type = "connections"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

[[services.ports]]
handlers = ["tls", "http"]
port = 443

[[services.tcp_checks]]
grace_period = "1s"
interval = "15s"
restart_limit = 0
timeout = "2s"
4 changes: 2 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
}
},
"scripts": {
"build": "react-scripts build",
"build": "",
"eject": "react-scripts eject",
"lint": "eslint \"**/*.{ts,tsx}\"",
"test": "jest --verbose",
"start-backend": "ts-node --experimental-specifier-resolution=node ./server.ts",
"start-backend-alt": "node --experimental-specifier-resolution=node --loader ts-node/esm ./server.ts",
"start": "node server.js",
"start": "node --experimental-specifier-resolution=node --loader ts-node/esm ./server.ts",
"client": "npm start --prefix ../frontend",
"dev": "concurrently \"npm run start-backend\" \"npm run client\""
},
Expand Down
5 changes: 5 additions & 0 deletions backend/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IUser {
last_name: string
username: string
password: string
history_clicks: number
}

const UserSchema = new Schema({
Expand All @@ -25,6 +26,10 @@ const UserSchema = new Schema({
type: String,
required: true
},
history_clicks: {
type: Number,
required: true
}
})

export const User = mongoose.model('users', UserSchema)
13 changes: 13 additions & 0 deletions backend/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ router.get("/:username", (req, res) => {
});
});

// UPDATE a Task
router.put("/:username", async (req, res) => {
const user = req.body;
console.log("update task", user, req.params.username)
User.findOneAndUpdate({ username: user.username}, { $set: user }, (err: any, result: any) => {
if (err) {
res.json(err);
} else {
res.json(result);
}
});
});

// CREATE a new User
router.post("/", async (req, res) => {
console.log('post request')
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/backend-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import mongoose from "mongoose";


let instance = axios.create({
baseURL: "http://localhost:8080/api/",
baseURL: "https://doneserver.fly.dev/api",
});


Expand Down Expand Up @@ -83,6 +83,11 @@ export async function addUser(user) {
}
}

export async function updateUser(user) {
const id = user.username
await instance.put("tasks/" + id, user).then((response) => response.data)
}

export async function authenticateUser(username: string, password: string) {
const response = await instance.get("users/" + username)
if (response.data.length == 0) {
Expand Down

0 comments on commit 9bff10b

Please sign in to comment.