Skip to content
This repository was archived by the owner on May 31, 2023. It is now read-only.

Commit 8444615

Browse files
committed
fix bug in updating studies
1 parent 94aa061 commit 8444615

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

src/sample-db-app/src/app/containers/app.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ export class AppComponent {
4848
showSidenav$: Observable<boolean>;
4949

5050
toolbarButtons = [
51-
{
52-
icon: 'arrow_back',
53-
action: back(),
54-
color: 'accent',
55-
tooltip: 'Go Back'
56-
}
51+
// {
52+
// icon: 'arrow_back',
53+
// action: back(),
54+
// color: 'accent',
55+
// tooltip: 'Go Back'
56+
// }
5757
];
5858

5959
constructor(private store: Store<fromRoot.State>) {

src/sample-db-app/src/app/effects/study.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { empty } from 'rxjs/observable/empty';
1313
import { of } from 'rxjs/observable/of';
1414
import { go, replace, search, show, back, forward } from '@ngrx/router-store';
1515

16-
import { Study } from '../models/study';
16+
import { Study, StudyEntry } from '../models/study';
1717
import { StudyService } from '../services/study';
1818
import * as study from '../actions/study';
1919

@@ -73,8 +73,8 @@ export class StudyEffects {
7373
.map(toPayload)
7474
.switchMap(payload => {
7575
return this.studyService.updateStudy(payload)
76-
.mergeMap((updatedStudy: Study) => {
77-
return [ new study.LoadOneAction({study: updatedStudy}), go('study') ]
76+
.mergeMap((updatedStudyEntry: StudyEntry) => {
77+
return [ new study.LoadOneAction(updatedStudyEntry), go('study') ];
7878
})
7979
.catch(err => {
8080
if (err.status < 500 && err.status > 400) {

src/sample-db-app/src/app/services/study.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,28 @@ export class StudyService {
6060
});
6161
}
6262

63-
createStudy(study: Study): Observable<Study> {
63+
createStudy(study: Study): Observable<StudyEntry> {
6464
return this.http.post(`${this.API_PATH}/study`, {'study': study}, {headers: this.headers})
6565
.map(res => res.json())
6666
.map(res => {
67-
if (Object.keys(res.error).length === 0) {
67+
if (Object.keys(res.error.study).length === 0 &&
68+
Object.keys(res.error.specimen).length === 0 &&
69+
Object.keys(res.error.study_subject).length === 0) {
6870
return res.data;
6971
} else {
7072
throw(new Error(JSON.stringify(res.error)));
7173
}
7274
})
73-
.map((study: Study) => {
74-
study.created = new Date(study.created);
75-
study.last_updated = new Date(study.last_updated);
76-
return study;
75+
.map((studyEntry: StudyEntry) => {
76+
studyEntry.study.created = new Date(studyEntry.study.created);
77+
studyEntry.study.last_updated = new Date(studyEntry.study.last_updated);
78+
studyEntry.specimen = studyEntry.specimen.map(specimen => {
79+
if (specimen.collection_date) {
80+
specimen.collection_date = new Date(specimen.collection_date);
81+
}
82+
return specimen;
83+
});
84+
return studyEntry;
7785
});
7886
}
7987

@@ -89,7 +97,7 @@ export class StudyService {
8997
});
9098
}
9199

92-
updateStudy(study: Study): Observable<Study> {
100+
updateStudy(study: Study): Observable<StudyEntry> {
93101
return this.createStudy(study);
94102
}
95103

src/sample-db-py/sample_db/app.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def create_study(self, title, short_code, is_longitudinal, lead_person, descript
7272
return study
7373

7474
def get_study(self, study_id):
75-
# type: (int) -> Tuple[Study, List[StudySubject], List[Specimen]]
75+
# type: (int) -> Tuple[Study, List[StudySubject], List[Specimen], List[MatrixTube]]
7676
"""
7777
Get a study
7878
:param study_id: study ID
@@ -91,28 +91,36 @@ def _get_study_by_short_code(session, short_code):
9191
return study
9292

9393
def edit_study(self, study):
94-
# type: (Study) -> Study
94+
# type: (Study) -> Tuple[Study, List[StudySubject], List[Specimen], List[MatrixTube]]
9595
with self._session_scope() as session:
9696
old_study = session.query(Study).get(study.id)
9797
old_study.title = study.title
9898
old_study.description = study.description
9999
old_study.short_code = study.short_code
100100
old_study.is_longitudinal = study.is_longitudinal
101101
old_study.lead_person = study.lead_person
102-
return old_study
102+
study_subjects = session.query(StudySubject).filter(StudySubject.study_id == old_study.id).all() # type: list[StudySubject]
103+
specimens = session.query(Specimen).join(StudySubject).filter(StudySubject.study_id == old_study.id).all() # type: list[Specimen]
104+
matrix_tubes = session.query(MatrixTube).join(Specimen).join(StudySubject).filter(StudySubject.study_id == old_study.id).all()
105+
return old_study, study_subjects, specimens, matrix_tubes
103106

104107
def update_study(self, id, d):
105-
# type: (int, dict) -> Study
108+
# type: (int, dict) -> Tuple[Study, List[StudySubject], List[Specimen], List[MatrixTube]]
106109
with self._session_scope() as session:
107110
with session.no_autoflush:
108111
study = session.query(Study).get(id)
109112
study.update(d)
110-
return study
113+
study_subjects = session.query(StudySubject).filter(
114+
StudySubject.study_id == study.id).all() # type: list[StudySubject]
115+
specimens = session.query(Specimen).join(StudySubject).filter(
116+
StudySubject.study_id == study.id).all() # type: list[Specimen]
117+
matrix_tubes = session.query(MatrixTube).join(Specimen).join(StudySubject).filter(
118+
StudySubject.study_id == study.id).all()
119+
return study, study_subjects, specimens, matrix_tubes
111120

112121
def delete_study(self, study):
113122
# type: (Study) -> boolean
114123
with self._session_scope() as session:
115-
print study
116124
s = session.query(Study).get(study.id)
117125
session.delete(s)
118126
return True

src/sample-db-py/sample_db/views.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,35 @@ def create_or_update_study():
107107
if not study.get('id'):
108108
try:
109109
study = db.create_study(**study)
110+
study_subjects = []
111+
specimens = []
112+
matrix_tubes = []
110113
except IntegrityError as e:
111114
raise InvalidUsage(parse_integrity_error(e), status_code=403)
112115
else:
113116
try:
114117
study_id = study.pop('id')
115-
study = db.update_study(study_id, study)
118+
study, study_subjects, specimens, matrix_tubes = db.update_study(study_id, study)
116119
except IntegrityError as e:
117120
raise InvalidUsage(parse_integrity_error(e), status_code=403)
118-
d, err = study_schema.dump(study)
119-
res = jsonify(sucess=True, data=d, error=err)
121+
study_entries, study_error = study_schema.dump(study)
122+
study_subject_entries, study_subject_error = study_subject_schema.dump(study_subjects, many=True)
123+
specimen_entries, specimen_error = specimen_schema.dump(specimens, many=True)
124+
matrix_tube_entries, matrix_tube_error = matrix_tube_schema.dump(matrix_tubes, many=True)
125+
print study_subject_entries, specimen_entries, matrix_tube_entries
126+
d = {
127+
'study': study_entries,
128+
'study_subject': study_subject_entries,
129+
'specimen': specimen_entries,
130+
'matrix_tube': matrix_tube_entries
131+
}
132+
err = {
133+
'study': study_error,
134+
'study_subject': study_subject_error,
135+
'specimen': specimen_error,
136+
'matrix_tube': matrix_tube_error
137+
}
138+
res = jsonify(data=d, error=err)
120139
return res
121140
except IntegrityError as e:
122141
raise InvalidUsage(parse_integrity_error(e), status_code=403)

0 commit comments

Comments
 (0)