Skip to content

Commit fa9668d

Browse files
authored
feat: removed nested study and user (#134)
* feat: removed nested study and user * chore: remove comments
1 parent 08ed224 commit fa9668d

File tree

3 files changed

+19
-78
lines changed

3 files changed

+19
-78
lines changed

src/database/StudyUser.database.go

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,19 @@ func baseGetStudyUsersById(query string, args ...interface{}) ([]models.StudyUse
2323
}
2424
}()
2525

26-
dbStudyUsers := []models.DBStudyUser{}
27-
studyUsers := []models.StudyUser{}
26+
retrievedStudyUsers := []models.StudyUser{}
2827

2928
httpStatus := baseRepositoryImpl.GetAllBy(
30-
&dbStudyUsers,
29+
&retrievedStudyUsers,
3130
query,
3231
args...,
3332
)
3433

3534
if !common.HTTPRequestIsSuccessful(httpStatus.Status) {
36-
return studyUsers, httpStatus
35+
return retrievedStudyUsers, httpStatus
3736
}
3837

39-
for _, dbStudyUser := range dbStudyUsers {
40-
study, getStudyHttpStatus := studyRepositoryImpl.GetStudyById(dbStudyUser.StudyID)
41-
if !common.HTTPRequestIsSuccessful(getStudyHttpStatus.Status) {
42-
return []models.StudyUser{}, getStudyHttpStatus
43-
}
44-
user, getUserHttpStatus := userRepositoryImpl.GetUserById(dbStudyUser.UserID)
45-
if !common.HTTPRequestIsSuccessful(getUserHttpStatus.Status) {
46-
return studyUsers, getUserHttpStatus
47-
}
48-
49-
studyUser := models.StudyUser{
50-
User: user,
51-
Study: study,
52-
CompletionCode: dbStudyUser.CompletionCode,
53-
RegisterDate: dbStudyUser.RegisterDate,
54-
DueDate: dbStudyUser.DueDate,
55-
CurrentTaskIndex: dbStudyUser.CurrentTaskIndex,
56-
HasAcceptedConsent: dbStudyUser.HasAcceptedConsent,
57-
Lang: dbStudyUser.Lang,
58-
Data: dbStudyUser.Data,
59-
}
60-
61-
studyUsers = append(studyUsers, studyUser)
62-
}
63-
64-
return studyUsers, models.HTTPStatus{Status: http.StatusOK, Message: http.StatusText(http.StatusOK)}
38+
return retrievedStudyUsers, models.HTTPStatus{Status: http.StatusOK, Message: http.StatusText(http.StatusOK)}
6539
}
6640

6741
// GetAllStudyUsersByStudyId retrieves all study users associated with the given user id from the database.
@@ -76,9 +50,9 @@ func (s *StudyUserRepository) GetAllStudyUsersByStudyId(studyId uint) ([]models.
7650

7751
return baseGetStudyUsersById(
7852
`
79-
SELECT user_id, study_id, completion_code, register_date, due_date, current_task_index, has_accepted_consent, lang, data
80-
FROM study_users
81-
WHERE study_id = ?;
53+
SELECT user_id, study_id, completion_code, register_date, due_date, current_task_index, has_accepted_consent, lang, data
54+
FROM study_users
55+
WHERE study_id = ?;
8256
`,
8357
studyId,
8458
)
@@ -121,8 +95,8 @@ func (s *StudyUserRepository) CreateStudyUser(studyUser *models.StudyUser) model
12195
(user_id, study_id, completion_code, current_task_index, register_date, due_date, has_accepted_consent, lang, data)
12296
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
12397
`,
124-
studyUser.User.ID,
125-
studyUser.Study.ID,
98+
studyUser.UserID,
99+
studyUser.StudyID,
126100
studyUser.CompletionCode,
127101
studyUser.CurrentTaskIndex,
128102
time.Now().UTC(),
@@ -163,11 +137,10 @@ func (s *StudyUserRepository) GetStudyUserByUserAndStudyId(userId uint, studyId
163137
}
164138
}()
165139

166-
dbStudyUser := models.DBStudyUser{}
167140
studyUser := models.StudyUser{}
168141

169142
if httpStatus := baseRepositoryImpl.GetOneBy(
170-
&dbStudyUser,
143+
&studyUser,
171144
`
172145
SELECT user_id, study_id, completion_code, register_date, due_date, current_task_index, has_accepted_consent, lang, data
173146
FROM study_users
@@ -179,24 +152,6 @@ func (s *StudyUserRepository) GetStudyUserByUserAndStudyId(userId uint, studyId
179152
return studyUser, httpStatus
180153
}
181154

182-
userForStudyUser, getUserHttpStatus := userRepositoryImpl.GetUserById(userId)
183-
if !common.HTTPRequestIsSuccessful(getUserHttpStatus.Status) {
184-
return models.StudyUser{}, getUserHttpStatus
185-
}
186-
studyForStudyUser, getStudyHttpStatus := studyRepositoryImpl.GetStudyById(studyId)
187-
if !common.HTTPRequestIsSuccessful(getUserHttpStatus.Status) {
188-
return models.StudyUser{}, getStudyHttpStatus
189-
}
190-
191-
studyUser.User = userForStudyUser
192-
studyUser.Study = studyForStudyUser
193-
studyUser.CompletionCode = dbStudyUser.CompletionCode
194-
studyUser.RegisterDate = dbStudyUser.RegisterDate
195-
studyUser.DueDate = dbStudyUser.DueDate
196-
studyUser.CurrentTaskIndex = dbStudyUser.CurrentTaskIndex
197-
studyUser.HasAcceptedConsent = dbStudyUser.HasAcceptedConsent
198-
studyUser.Lang = dbStudyUser.Lang
199-
studyUser.Data = dbStudyUser.Data
200155
return studyUser, models.HTTPStatus{Status: http.StatusOK, Message: http.StatusText(http.StatusOK)}
201156
}
202157

@@ -224,8 +179,8 @@ func (s *StudyUserRepository) UpdateStudyUser(studyUser *models.StudyUser) model
224179
studyUser.HasAcceptedConsent,
225180
studyUser.Lang,
226181
studyUser.Data,
227-
studyUser.Study.ID,
228-
studyUser.User.ID,
182+
studyUser.StudyID,
183+
studyUser.UserID,
229184
); err != nil {
230185
httpStatus := models.HTTPStatus{
231186
Status: http.StatusInternalServerError,
@@ -246,15 +201,15 @@ func (s *StudyUserRepository) UpdateStudyUser(studyUser *models.StudyUser) model
246201

247202
// GetAllStudyUsers gets all study users for the database. This is used for the summary.
248203
// It returns a 200 or 500 status code.
249-
func (u *StudyUserRepository) GetAllStudyUsers() ([]models.DBStudyUser, models.HTTPStatus) {
204+
func (u *StudyUserRepository) GetAllStudyUsers() ([]models.StudyUser, models.HTTPStatus) {
250205
axonlogger.InfoLogger.Println("STUDYUSER DATABASE: GetAllStudyUsers()")
251206
defer func() {
252207
if err := recover(); err != nil {
253208
axonlogger.ErrorLogger.Println("there was an error updating the user", err)
254209
}
255210
}()
256211

257-
dbStudyUsers := []models.DBStudyUser{}
212+
dbStudyUsers := []models.StudyUser{}
258213
httpStatus := baseRepositoryImpl.GetAllBy(
259214
&dbStudyUsers,
260215
`SELECT user_id, study_id, completion_code, register_date, due_date, current_task_index, has_accepted_consent, lang, data

src/models/StudyUser.model.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,8 @@ var StudyUserSchema = `
2323
);
2424
`
2525

26-
// StudyUser represents a join table between studys and users keeping track of the progress
27-
// for that user in each study that the user is part of
26+
// StudyUser represents a join table between studies and users, tracking the progress and registration of users within a study
2827
type StudyUser struct {
29-
User User `json:"user"`
30-
Study Study `json:"study"`
31-
CompletionCode string `json:"completionCode"`
32-
RegisterDate time.Time `json:"registerDate"`
33-
DueDate sql.NullTime `json:"dueDate"`
34-
CurrentTaskIndex int `json:"currentTaskIndex"`
35-
HasAcceptedConsent bool `json:"hasAcceptedConsent"`
36-
Lang string `json:"lang"`
37-
Data MapStringInterface `json:"data"`
38-
}
39-
40-
// DBStudyUser is the internal database representation of the study user
41-
type DBStudyUser struct {
4228
UserID uint `json:"userId"`
4329
StudyID uint `json:"studyId"`
4430
CompletionCode string `json:"completionCode"`

src/services/StudyUser.service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func (s *StudyUserService) UpdateStudyUser(userId string, studyId string, studyU
6161
return models.StudyUser{}, models.HTTPStatus{Status: http.StatusInternalServerError, Message: http.StatusText(http.StatusInternalServerError)}
6262
}
6363

64-
if parsedUserId != studyUser.User.ID || parsedStudyId != studyUser.Study.ID {
65-
axonlogger.WarningLogger.Println("Given id is not equivalent. User ID Param:", userId, "StudyUser User ID:", studyUser.User.ID, "Study ID Param:", studyId, "StudyUser Study ID:", studyUser.Study.ID)
64+
if parsedUserId != studyUser.UserID || parsedStudyId != studyUser.StudyID {
65+
axonlogger.WarningLogger.Println("Given id is not equivalent. User ID Param:", userId, "StudyUser User ID:", studyUser.UserID, "Study ID Param:", studyId, "StudyUser Study ID:", studyUser.StudyID)
6666
return models.StudyUser{}, models.HTTPStatus{Status: http.StatusForbidden, Message: http.StatusText(http.StatusForbidden)}
6767
}
6868

@@ -96,10 +96,10 @@ func (s *StudyUserService) UpdateStudyUser(userId string, studyId string, studyU
9696
// i.e. ParticipantData.Data[studyUser.currentTaskIndex] should exist.
9797
// This function also returns false if it encounters an error.
9898
func allowStudyUserCurrentTaskIndexIncrement(studyUser models.StudyUser) bool {
99-
userIdAsString := common.ConvertUintToString(studyUser.User.ID)
99+
userIdAsString := common.ConvertUintToString(studyUser.UserID)
100100

101101
_, httpStatus := participantDataRepositoryImpl.GetParticipantDataByStudyAndUserIdAndTaskOrder(
102-
studyUser.Study.ID,
102+
studyUser.StudyID,
103103
userIdAsString,
104104
studyUser.CurrentTaskIndex,
105105
)

0 commit comments

Comments
 (0)