-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Son Nguyen
committed
Dec 17, 2023
1 parent
41d0ba4
commit 3e5eb39
Showing
16 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
CREATE DATABASE weathermate; | ||
USE weathermate; | ||
|
||
CREATE TABLE users ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
username VARCHAR(50) NOT NULL UNIQUE, | ||
password VARCHAR(255) NOT NULL, | ||
email VARCHAR(100) NOT NULL UNIQUE | ||
); | ||
|
||
CREATE TABLE weather_data ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
city VARCHAR(100) NOT NULL, | ||
data JSON NOT NULL, | ||
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS users ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
username VARCHAR(50) NOT NULL UNIQUE, | ||
password VARCHAR(255) NOT NULL, | ||
email VARCHAR(100) NOT NULL UNIQUE | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS user_preferences ( | ||
user_id INT NOT NULL, | ||
city VARCHAR(100) NOT NULL, | ||
alert_type VARCHAR(50), | ||
FOREIGN KEY (user_id) REFERENCES users(id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.db import models | ||
|
||
class WeatherData(models.Model): | ||
city = models.CharField(max_length=100) | ||
data = models.JSONField() | ||
timestamp = models.DateTimeField(auto_now_add=True) | ||
|
||
def __str__(self): | ||
return self.city | ||
|
||
class UserPreference(models.Model): | ||
user = models.ForeignKey('auth.User', on_delete=models.CASCADE) | ||
city = models.CharField(max_length=100) | ||
alert_type = models.CharField(max_length=50) | ||
active = models.BooleanField(default=True) | ||
|
||
def __str__(self): | ||
return f"{self.user.username}'s preference" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from rest_framework import serializers | ||
from .models import WeatherData | ||
|
||
class WeatherDataSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = WeatherData | ||
fields = ['city', 'data', 'timestamp'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
from . import views | ||
|
||
router = DefaultRouter() | ||
router.register(r'weatherdata', views.WeatherDataViewSet) | ||
|
||
urlpatterns = [ | ||
path('', include(router.urls)), | ||
path('current/<str:city>/', views.current_weather), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.http import JsonResponse | ||
from .models import WeatherData, UserPreference | ||
from .serializers import WeatherDataSerializer | ||
from rest_framework import viewsets | ||
from rest_framework.response import Response | ||
from rest_framework.decorators import api_view | ||
|
||
class WeatherDataViewSet(viewsets.ModelViewSet): | ||
queryset = WeatherData.objects.all() | ||
serializer_class = WeatherDataSerializer | ||
|
||
@api_view(['GET']) | ||
def current_weather(request, city): | ||
weather = WeatherData.objects.filter(city=city).order_by('-timestamp').first() | ||
if weather: | ||
return Response({'city': weather.city, 'data': weather.data}) | ||
return Response({'error': 'Weather data not found'}, status=404) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.contrib import admin | ||
from django.urls import path, include | ||
|
||
urlpatterns = [ | ||
path('admin/', admin.site.urls), | ||
path('api/', include('weather.urls')), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
include 'config.php'; | ||
|
||
if ($_SERVER["REQUEST_METHOD"] == "POST") { | ||
$username = $_POST["username"]; | ||
$password = password_hash($_POST["password"], PASSWORD_DEFAULT); | ||
$email = $_POST["email"]; | ||
|
||
try { | ||
$stmt = $conn->prepare("INSERT INTO users (username, password, email) VALUES (?, ?, ?)"); | ||
$stmt->execute([$username, $password, $email]); | ||
echo "User registered successfully"; | ||
} | ||
catch(PDOException $e) { | ||
echo "Error: " . $e->getMessage(); | ||
} | ||
} | ||
|
||
if ($_SERVER["REQUEST_METHOD"] == "GET") { | ||
$username = $_GET["username"]; | ||
$password = $_GET["password"]; | ||
|
||
try { | ||
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?"); | ||
$stmt->execute([$username]); | ||
$user = $stmt->fetch(); | ||
|
||
if (password_verify($password, $user["password"])) { | ||
echo "Login successful"; | ||
} else { | ||
echo "Login failed"; | ||
} | ||
} | ||
catch(PDOException $e) { | ||
echo "Error: " . $e->getMessage(); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
include 'db_config.php'; | ||
|
||
session_start(); | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) { | ||
$userId = $_SESSION['user_id']; // Assume user ID is stored in session | ||
$newEmail = $_POST['email']; // Example field to update | ||
|
||
$stmt = $pdo->prepare("UPDATE users SET email = ? WHERE id = ?"); | ||
$stmt->execute([$newEmail, $userId]); | ||
|
||
echo "Profile updated successfully"; | ||
} | ||
|
||
if (isset($_SESSION['user_id'])) { | ||
$userId = $_SESSION['user_id']; // Assume user ID is stored in session | ||
|
||
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?"); | ||
$stmt->execute([$userId]); | ||
$user = $stmt->fetch(); | ||
|
||
echo "Welcome " . $user['name']; | ||
} | ||
else { | ||
echo "You are not logged in"; | ||
} | ||
|
||
if (isset($_SESSION['user_id'])) { | ||
echo "You are logged in"; | ||
} | ||
else { | ||
echo "You are not logged in"; | ||
} | ||
?> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
include 'db_config.php'; | ||
|
||
function getWeatherForecast($city) { | ||
$apiKey = 'YOUR_API_KEY'; // Replace with your API key | ||
$apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=$city&appid=$apiKey"; | ||
|
||
$response = file_get_contents($apiUrl); | ||
return json_decode($response, true); | ||
} | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'GET') { | ||
$city = $_GET['city']; // Example: get city from query parameter | ||
$forecast = getWeatherForecast($city); | ||
echo json_encode($forecast); | ||
} | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
$city = $_POST['city']; // Example: get city from form data | ||
$forecast = getWeatherForecast($city); | ||
echo json_encode($forecast); | ||
} | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'PUT') { | ||
parse_str(file_get_contents("php://input"), $put_vars); | ||
$city = $put_vars['city']; // Example: get city from PUT data | ||
$forecast = getWeatherForecast($city); | ||
echo json_encode($forecast); | ||
} | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') { | ||
parse_str(file_get_contents("php://input"), $delete_vars); | ||
$city = $delete_vars['city']; // Example: get city from DELETE data | ||
$forecast = getWeatherForecast($city); | ||
echo json_encode($forecast); | ||
} | ||
?> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
$host = 'localhost'; | ||
$dbname = 'weathermate'; | ||
$username = 'root'; | ||
$password = '123xyz'; | ||
|
||
try { | ||
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); | ||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
} | ||
catch(PDOException $e) { | ||
echo "Connection failed: " . $e->getMessage(); | ||
} | ||
|
||
if (isset($_POST['submit'])) { | ||
$name = $_POST['name']; | ||
$email = $_POST['email']; | ||
$message = $_POST['message']; | ||
|
||
try { | ||
$sql = "INSERT INTO contact (name, email, message) VALUES ('$name', '$email', '$message')"; | ||
$conn->exec($sql); | ||
echo "New record created successfully"; | ||
} | ||
catch(PDOException $e) { | ||
echo $sql . "<br>" . $e->getMessage(); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
$host = 'localhost'; | ||
$dbname = 'weathermate'; | ||
$username = 'root'; | ||
$password = ''; | ||
|
||
try { | ||
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); | ||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
} | ||
catch (PDOException $e) { | ||
die("Database connection failed: " . $e->getMessage()); | ||
} | ||
|
||
if (isset($_POST['submit'])) { | ||
$name = $_POST['name']; | ||
$email = $_POST['email']; | ||
$message = $_POST['message']; | ||
|
||
try { | ||
$sql = "INSERT INTO contact (name, email, message) VALUES ('$name', '$email', '$message')"; | ||
$pdo->exec($sql); | ||
echo "Message sent successfully"; | ||
} | ||
catch (PDOException $e) { | ||
die("Message sending failed: " . $e->getMessage()); | ||
} | ||
} | ||
|
||
if (isset($_POST['submit2'])) { | ||
$name = $_POST['name']; | ||
$email = $_POST['email']; | ||
$message = $_POST['message']; | ||
|
||
try { | ||
$sql = "INSERT INTO subscribe (name, email, message) VALUES ('$name', '$email', '$message')"; | ||
$pdo->exec($sql); | ||
echo "Message sent successfully"; | ||
} | ||
catch (PDOException $e) { | ||
die("Message sending failed: " . $e->getMessage()); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
include 'db_config.php'; | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
$username = $_POST['username']; | ||
$password = $_POST['password']; | ||
|
||
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?"); | ||
$stmt->execute([$username]); | ||
$user = $stmt->fetch(); | ||
|
||
if ($user && password_verify($password, $user['password'])) { | ||
echo "Login successful"; | ||
} | ||
else { | ||
echo "Invalid credentials"; | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
include 'config.php'; | ||
|
||
$city = 'city_name'; | ||
$apiKey = 'none'; | ||
$apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=$city&appid=$apiKey"; | ||
|
||
$weatherData = file_get_contents($apiUrl); | ||
$weatherData = json_decode($weatherData, true); | ||
|
||
try { | ||
$stmt = $conn->prepare("INSERT INTO weather_data (city, data) VALUES (?, ?)"); | ||
$stmt->execute([$city, json_encode($weatherData)]); | ||
echo "Weather data stored successfully"; | ||
} | ||
catch(PDOException $e) { | ||
echo "Error: " . $e->getMessage(); | ||
} | ||
|
||
$conn = null; | ||
|
||
?> |