Example #1
0
        public IActionResult CreateReplaceProgressRecord(
            [FromBody]  ProgressRecordForCreationDto newProgressRecord)
        {
            if (newProgressRecord == null)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            if (!_repo.StudentExists(newProgressRecord.StudentId) ||
                !_repo.WeekExists(newProgressRecord.WeekId) ||
                !_repo.TrackedItemExists(newProgressRecord.TrackedItemId))
            {
                return(NotFound());
            }

            var finalTrackedItem = _mapper.Map <Entities.ProgressRecord>(newProgressRecord);

            //START: DUPLICATE ITEM CHECK
            var existingProgressRecord = _repo.GetProgressRecordByForeignKeys(
                finalTrackedItem.ClassEntityId,
                finalTrackedItem.StudentId,
                finalTrackedItem.TrackedItemId,
                finalTrackedItem.WeekId);

            if (existingProgressRecord != null)
            {
                _repo.DeleteProgressRecord(existingProgressRecord);
                //return UnprocessableEntity("There is already a Progress Record with those Id's");
            }
            //END: DUPLICATE ITEM CHECK


            _repo.AddProgressRecord(finalTrackedItem);
            if (!_repo.Save())
            {
                return(StatusCode(500, $"A problem happened while handling your request to create a progress record with these Foreign Keys:" +
                                  $"\nClassEntityId:{newProgressRecord.ClassEntityId}" +
                                  $"\nStudentId:{newProgressRecord.StudentId}" +
                                  $"\nTrackedItemId:{newProgressRecord.TrackedItemId}" +
                                  $"\nWeekId:{newProgressRecord.WeekId}"));
            }
            var progressRecordToReturn = _mapper.Map <ProgressRecordDto>(finalTrackedItem);

            return(CreatedAtAction("GetProgressRecord", new
                                   { progressRecordId = progressRecordToReturn.Id }, progressRecordToReturn));
        }
Example #2
0
        public async Task <ProgressRecordDto> AddOrReplaceProgressRecord(ProgressRecordForCreationDto progressRecord)
        {
            var progressRecordForCreation =
                new StringContent(JsonSerializer.Serialize(progressRecord), Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync($"api/classentities/progressrecords", progressRecordForCreation);

            if (response.IsSuccessStatusCode)
            {
                return(await JsonSerializer.DeserializeAsync <ProgressRecordDto>(await response.Content.ReadAsStreamAsync()));
            }

            return(null);
        }