-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileService.cs
More file actions
142 lines (118 loc) · 5.41 KB
/
Copy pathProfileService.cs
File metadata and controls
142 lines (118 loc) · 5.41 KB
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
139
140
141
142
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace H1
{
public partial class ProfileService : Form
{
public TravelerProfile GetTravelerProfile(int userId)
{
TravelerProfile profile = null;
string query = @"SELECT tp.profile_id, tp.user_id, tp.first_name, tp.last_name, u.email,
tp.phone, tp.profile_picture, tp.date_of_birth, tp.nationality, tp.created_at
FROM TravelerProfile tp
JOIN AppUser u ON tp.user_id = u.user_id
WHERE tp.user_id = @UserId";
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("@UserId", userId)
};
DataTable result = DatabaseUtil.ExecuteDataTable(query, CommandType.Text, parameters);
if (result.Rows.Count > 0)
{
DataRow row = result.Rows[0];
profile = new TravelerProfile
{
ProfileId = Convert.ToInt32(row["profile_id"]),
UserId = Convert.ToInt32(row["user_id"]),
FirstName = row["first_name"].ToString(),
LastName = row["last_name"].ToString(),
Email = row["email"].ToString(),
Phone = row["phone"].ToString(),
ProfilePicture = row["profile_picture"] != DBNull.Value ? row["profile_picture"].ToString() : null,
DateOfBirth = row["date_of_birth"] != DBNull.Value ? Convert.ToDateTime(row["date_of_birth"]) : (DateTime?)null,
Nationality = row["nationality"] != DBNull.Value ? row["nationality"].ToString() : null,
CreatedAt = Convert.ToDateTime(row["created_at"]),
Preferences = GetTravelerPreferences(userId)
};
}
return profile;
}
// Get traveler preferences by user ID
public List<string> GetTravelerPreferences(int userId)
{
List<string> preferences = new List<string>();
string query = @"SELECT preference_value
FROM TravelerPreference
WHERE user_id = @UserId AND preference_type = 'Category'";
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("@UserId", userId)
};
DataTable result = DatabaseUtil.ExecuteDataTable(query, CommandType.Text, parameters);
foreach (DataRow row in result.Rows)
{
preferences.Add(row["preference_value"].ToString());
}
return preferences;
}
// Update traveler profile
public bool UpdateTravelerProfile(TravelerProfile profile)
{
string query = @"UPDATE TravelerProfile
SET first_name = @FirstName,
last_name = @LastName,
phone = @Phone,
date_of_birth = @DOB,
nationality = @Nationality
WHERE user_id = @UserId";
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("@FirstName", profile.FirstName),
new SqlParameter("@LastName", profile.LastName),
new SqlParameter("@Phone", profile.Phone),
new SqlParameter("@DOB", profile.DateOfBirth ?? (object)DBNull.Value),
new SqlParameter("@Nationality", !string.IsNullOrEmpty(profile.Nationality) ? profile.Nationality : (object)DBNull.Value),
new SqlParameter("@UserId", profile.UserId)
};
int result = DatabaseUtil.ExecuteNonQuery(query, CommandType.Text, parameters);
if (result > 0)
{
// Update preferences
return UpdateTravelerPreferences(profile.UserId, profile.Preferences);
}
return false;
}
// Update traveler preferences
public bool UpdateTravelerPreferences(int userId, List<string> preferences)
{
// First, delete existing preferences
string deleteQuery = "DELETE FROM TravelerPreference WHERE user_id = @UserId AND preference_type = 'Category'";
SqlParameter[] deleteParams = new SqlParameter[]
{
new SqlParameter("@UserId", userId)
};
DatabaseUtil.ExecuteNonQuery(deleteQuery, CommandType.Text, deleteParams);
// Then, insert new preferences
foreach (string preference in preferences)
{
string insertQuery = @"INSERT INTO TravelerPreference (user_id, preference_type, preference_value)
VALUES (@UserId, 'Category', @Value)";
SqlParameter[] insertParams = new SqlParameter[]
{
new SqlParameter("@UserId", userId),
new SqlParameter("@Value", preference)
};
DatabaseUtil.ExecuteNonQuery(insertQuery, CommandType.Text, insertParams);
}
return true;
}
}
}