File size: 4,153 Bytes
f2e1f0c
 
 
 
 
 
 
 
 
 
 
 
 
 
625a0da
 
 
f2e1f0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
from pymongo import MongoClient
from datetime import datetime
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# MongoDB Atlas connection string
MONGO_URI = os.environ.get('MONGO_URI')
DB_NAME = os.environ.get('MONGO_DB_NAME', 'student_reports')

def get_db_connection():
    try:
        client = MongoClient(MONGO_URI, serverSelectionTimeoutMS=5000)
        # Test the connection
        client.server_info()
        db = client[DB_NAME]
        return db
    except Exception as e:
        print(f"Error connecting to MongoDB: {e}")
        return None

def get_all_mahasiswa():
    db = get_db_connection()
    if db is None:
        return []
    
    try:
        # Convert MongoDB documents to dictionaries
        mahasiswa = list(db.mahasiswa.find({}, {'_id': 0}))
        
        # Convert string dates to datetime objects for compatibility with the app
        for student in mahasiswa:
            if isinstance(student['tanggal_lahir'], str):
                student['tanggal_lahir'] = datetime.strptime(student['tanggal_lahir'], '%Y-%m-%d')
        
        return mahasiswa
    except Exception as e:
        print(f"Error fetching data: {e}")
        return []

def add_mahasiswa(NRP, nama_lengkap, no_hp, tanggal_lahir):
    db = get_db_connection()
    if db is None:
        return False

    try:
        # Create a document
        student = {
            "NRP": NRP,
            "nama_lengkap": nama_lengkap,
            "no_hp": no_hp,
            "tanggal_lahir": datetime.strptime(tanggal_lahir, '%Y-%m-%d')
        }
        
        # Insert into collection
        db.mahasiswa.insert_one(student)
        return True
    except Exception as e:
        print(f"Error adding data: {e}")
        return False

def get_mahasiswa_by_id(id):
    db = get_db_connection()
    if db is None:
        return None
    
    try:
        # Find student by NRP/NIM
        student = db.mahasiswa.find_one({"NRP": id})
        
        # If not found with NRP, try with nim
        if student is None:
            student = db.mahasiswa.find_one({"nim": id})
            
        if student:
            # Remove MongoDB _id field before returning
            student.pop('_id', None)
            
            # Convert string dates to datetime objects if needed
            if isinstance(student['tanggal_lahir'], str):
                student['tanggal_lahir'] = datetime.strptime(student['tanggal_lahir'], '%Y-%m-%d')
                
        return student
    except Exception as e:
        print(f"Error fetching student: {e}")
        return None

def update_mahasiswa(id, nama_lengkap, no_hp, tanggal_lahir):
    db = get_db_connection()
    if db is None:
        return False
    
    try:
        # Find if student exists with NRP or nim
        student = db.mahasiswa.find_one({"NRP": id})
        id_field = "NRP"
        
        if student is None:
            student = db.mahasiswa.find_one({"nim": id})
            id_field = "nim"
            
        if student is None:
            return False
            
        # Update the document
        result = db.mahasiswa.update_one(
            {id_field: id},
            {"$set": {
                "nama_lengkap": nama_lengkap,
                "no_hp": no_hp,
                "tanggal_lahir": datetime.strptime(tanggal_lahir, '%Y-%m-%d')
            }}
        )
        
        return result.modified_count > 0
    except Exception as e:
        print(f"Error updating data: {e}")
        return False

def delete_mahasiswa(id):
    db = get_db_connection()
    if db is None:
        return False
    
    try:
        # Try to delete by NRP
        result = db.mahasiswa.delete_one({"NRP": id})
        
        # If nothing was deleted, try with nim
        if result.deleted_count == 0:
            result = db.mahasiswa.delete_one({"nim": id})
            
        return result.deleted_count > 0
    except Exception as e:
        print(f"Error deleting data: {e}")
        return False