Skip to content

Commit

Permalink
Create user objects via UI (#42)
Browse files Browse the repository at this point in the history
* successfully created student object via html

* added create button

* successfully create instructors via html

* successfully created parent object via html

* prepping pull request
  • Loading branch information
APandamonium1 authored Aug 11, 2024
1 parent 1cf632b commit 5306896
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 0 deletions.
Binary file modified EduSync.exe
Binary file not shown.
106 changes: 106 additions & 0 deletions adminHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func AdminHandler(router *mux.Router) {
t.Execute(res, nil)
}).Methods("GET")

//searve the search student page
router.HandleFunc("/admin/search_student", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/search_student.html")
if err != nil {
Expand All @@ -40,6 +41,16 @@ func AdminHandler(router *mux.Router) {
t.Execute(res, nil)
}).Methods("GET")

// Serve the create student page
router.HandleFunc("/admin/create_student", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/create_student.html")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(res, nil)
}).Methods("GET")

// Search for students by name and class
router.HandleFunc("/admin/api/search_student", func(res http.ResponseWriter, req *http.Request) {
name := req.URL.Query().Get("name")
Expand Down Expand Up @@ -113,6 +124,31 @@ func AdminHandler(router *mux.Router) {
// Request Body: JSON object with updated student information
// Response: HTTP Status No Content (204)

// Create a new student
router.HandleFunc("/admin/student/", func(res http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPost:
var student Student // Replace 'Student' with your actual student struct type
if err := json.NewDecoder(req.Body).Decode(&student); err != nil {
http.Error(res, fmt.Sprintf(`{"error": "Invalid request payload: %v"}`, err), http.StatusBadRequest)
return
}
student.GoogleID = uuid.New().String()
student.Role = "Student"
student.CreatedAt = time.Now()
student.UpdatedAt = time.Now()
if err := createStudent(student, req); err != nil { // Implement createStudent to handle the logic
http.Error(res, fmt.Sprintf(`{"error": "Failed to create student: %v"}`, err), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusCreated)
json.NewEncoder(res).Encode(student)
default:
http.Error(res, `{"error": "Invalid request method"}`, http.StatusMethodNotAllowed)
}
}).Methods("POST")

// Serve the search parent page
router.HandleFunc("/admin/search_parent", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/search_parent.html")
if err != nil {
Expand All @@ -122,6 +158,16 @@ func AdminHandler(router *mux.Router) {
t.Execute(res, nil)
}).Methods("GET")

// Serve the create parent page
router.HandleFunc("/admin/create_parent", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/create_parent.html")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(res, nil)
}).Methods("GET")

router.HandleFunc("/admin/api/search_parent", func(res http.ResponseWriter, req *http.Request) {
name := req.URL.Query().Get("name")
parents, err := searchParents(name)
Expand Down Expand Up @@ -178,6 +224,31 @@ func AdminHandler(router *mux.Router) {
}
}).Methods("GET", "PUT")

// Create a new parent
router.HandleFunc("/admin/parent/", func(res http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPost:
var parent Parent
if err := json.NewDecoder(req.Body).Decode(&parent); err != nil {
http.Error(res, fmt.Sprintf(`{"error": "Invalid request payload: %v"}`, err), http.StatusBadRequest)
return
}
parent.GoogleID = uuid.New().String()
parent.Role = "Parent"
parent.CreatedAt = time.Now()
parent.UpdatedAt = time.Now()
if err := createParent(parent, req); err != nil {
http.Error(res, fmt.Sprintf(`{"error": "Failed to create parent: %v"}`, err), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusCreated)
json.NewEncoder(res).Encode(parent)
default:
http.Error(res, `{"error": "Invalid request method"}`, http.StatusMethodNotAllowed)
}
}).Methods("POST")

// Serve the search instructor page
router.HandleFunc("/admin/search_instructor", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/search_instructor.html")
if err != nil {
Expand All @@ -187,6 +258,17 @@ func AdminHandler(router *mux.Router) {
t.Execute(res, nil)
}).Methods("GET")

// Serve the create instructor page
router.HandleFunc("/admin/create_instructor", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/create_instructor.html")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(res, nil)
}).Methods("GET")

// Serve the search instructor API endpoint
router.HandleFunc("/admin/api/search_instructor", func(res http.ResponseWriter, req *http.Request) {
name := req.URL.Query().Get("name")
instructors, err := searchInstructors(name)
Expand Down Expand Up @@ -243,6 +325,30 @@ func AdminHandler(router *mux.Router) {
}
}).Methods("GET", "PUT")

// Create a new instructor
router.HandleFunc("/admin/instructor/", func(res http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPost:
var instructor Instructor
if err := json.NewDecoder(req.Body).Decode(&instructor); err != nil {
http.Error(res, fmt.Sprintf(`{"error": "Invalid request payload: %v"}`, err), http.StatusBadRequest)
return
}
instructor.GoogleID = uuid.New().String()
instructor.Role = "Instructor"
instructor.CreatedAt = time.Now()
instructor.UpdatedAt = time.Now()
if err := createInstructor(instructor, req); err != nil {
http.Error(res, fmt.Sprintf(`{"error": "Failed to create instructor: %v"}`, err), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusCreated)
json.NewEncoder(res).Encode(instructor)
default:
http.Error(res, `{"error": "Invalid request method"}`, http.StatusMethodNotAllowed)
}
}).Methods("POST")

//Serve the search announcement page
router.HandleFunc("/admin/search_announcement", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/search_announcement.html")
Expand Down
97 changes: 97 additions & 0 deletions templates/admin/create_instructor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!DOCTYPE html>
<html>
<head>
<title>EduSync</title>
<meta name="viewport" content="width=device-width">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/assets/css/owl.carousel.css">
<link rel="stylesheet" type="text/css" href="/assets/css/inner-page-style.css">
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
<link href="https://fonts.googleapis.com/css?family=Raleway:400,500,600,700" rel="stylesheet">
<script>
async function createInstructor() {
const basePay = document.getElementById('basePay').value;

if (basePay < 0) {
alert('Base pay must be a positive number.');
return;
}

const newInstructor = {
name: document.getElementById('name').value,
email: document.getElementById('email').value,
contact_number: document.getElementById('contactNumber').value,
role: "Instructor",
base_pay: parseFloat(basePay),
number_of_students: parseInt(document.getElementById('numberOfStudents').value, 10)
};

await fetch(`/admin/instructor/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(newInstructor)
});

alert('Instructor created successfully');
window.location.href = '/admin/search_instructor';
}

function goBack() {
window.history.back();
}
</script>
</head>
<body>
<div id="page" class="site">
<header class="site-header">
<div class="main-header">
<div class="container">
<div class="logo-wrap">
<img src="/assets/images/site-logo.jpg" alt="Logo Image" style="width: 120px; height: auto;">
</div>
<div class="nav-wrap">
<nav class="nav-desktop">
<ul class="menu-list">
<li><a href="/admin">Home</a></li>
<li><a href="/admin/search_instructor">Instructors</a></li>
<li><a href="/admin/search_student">Students</a></li>
<li><a href="/admin/search_parent">Parents</a></li>
<li><a href="/admin/search_announcement">Announcements</a></li>
</ul>
</nav>
</div>
</div>
</div>
</header>

<div class="container content">
<h1>Create New Instructor</h1>
<form onsubmit="event.preventDefault(); createInstructor();">
<div><label>Name: </label><input type="text" id="name" required></div>
<br>
<div><label>Email: </label><input type="email" id="email" required></div>
<br>
<div><label>Contact Number: </label><input type="text" id="contactNumber" required></div>
<br>
<div><label>Base Pay: </label><input type="number" id="basePay" step="0.01" required></div>
<br>
<div><label>Number of Students: </label><input type="number" id="numberOfStudents" min="0" required></div>
<br>
<div class="button-container">
<button type="submit">Create Instructor</button>
<button type="button" onclick="goBack()">Back</button>
</div>
</form>
</div>
</div>
<script type="text/javascript" src="/assets/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/assets/js/all.js"></script>
<script type="text/javascript" src="/assets/js/isotope.pkgd.min.js"></script>
<script type="text/javascript" src="/assets/js/owl.carousel.js"></script>
<script type="text/javascript" src="/assets/js/jquery.flexslider.js"></script>
<script type="text/javascript" src="/assets/js/jquery.rateyo.js"></script>
<script type="text/javascript" src="/assets/js/custom.js"></script>
</body>
</html>
84 changes: 84 additions & 0 deletions templates/admin/create_parent.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<!DOCTYPE html>
<html>
<head>
<title>EduSync</title>
<meta name="viewport" content="width=device-width">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/assets/css/owl.carousel.css">
<link rel="stylesheet" type="text/css" href="/assets/css/inner-page-style.css">
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
<link href="https://fonts.googleapis.com/css?family=Raleway:400,500,600,700" rel="stylesheet">
<script>
async function createParent() {
const newParent = {
name: document.getElementById('name').value,
email: document.getElementById('email').value,
contact_number: document.getElementById('contactNumber').value,
role: "Parent"
};

await fetch(`/admin/parent/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(newParent)
});

alert('Parent created successfully');
window.location.href = '/admin/search_parent';
}

function goBack() {
window.history.back();
}
</script>
</head>
<body>
<div id="page" class="site">
<header class="site-header">
<div class="main-header">
<div class="container">
<div class="logo-wrap">
<img src="/assets/images/site-logo.jpg" alt="Logo Image" style="width: 120px; height: auto;">
</div>
<div class="nav-wrap">
<nav class="nav-desktop">
<ul class="menu-list">
<li><a href="/admin">Home</a></li>
<li><a href="/admin/search_instructor">Instructors</a></li>
<li><a href="/admin/search_student">Students</a></li>
<li><a href="/admin/search_parent">Parents</a></li>
<li><a href="/admin/search_announcement">Announcements</a></li>
</ul>
</nav>
</div>
</div>
</div>
</header>

<div class="container content">
<h1>Create New Parent</h1>
<form onsubmit="event.preventDefault(); createParent();">
<div><label>Name: </label><input type="text" id="name" required></div>
<br>
<div><label>Email: </label><input type="email" id="email" required></div>
<br>
<div><label>Contact Number: </label><input type="text" id="contactNumber" required></div>
<br>
<div class="button-container">
<button type="submit">Create Parent</button>
<button type="button" onclick="goBack()">Back</button>
</div>
</form>
</div>
</div>
<script type="text/javascript" src="/assets/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/assets/js/all.js"></script>
<script type="text/javascript" src="/assets/js/isotope.pkgd.min.js"></script>
<script type="text/javascript" src="/assets/js/owl.carousel.js"></script>
<script type="text/javascript" src="/assets/js/jquery.flexslider.js"></script>
<script type="text/javascript" src="/assets/js/jquery.rateyo.js"></script>
<script type="text/javascript" src="/assets/js/custom.js"></script>
</body>
</html>
Loading

0 comments on commit 5306896

Please sign in to comment.