예제 #1
0
        /// <summary>
        /// Updates the exam
        /// </summary>
        /// <param name="examUpdateInfo">Exam update information</param>
        /// <param name="userId">User id</param>
        /// <returns>update result</returns>
        public async Task <UpdateResult> Update(ExamUpdateInfo examUpdateInfo, int userId)
        {
            var exam = await this.GetExamById(examUpdateInfo.ExamId);

            if (exam == null)
            {
                return(UpdateResult.NotFound);
            }

            if (exam.CreatorUserId != userId || !exam.Modifiers.Contains(userId))
            {
                return(UpdateResult.NoAccess);
            }

            if (exam.Created.AddMinutes(Convert.ToDouble(exam.Duration)) < DateTime.Now)
            {
                return(UpdateResult.ExamEnded);
            }

            var updateDefBuilder = Builders <Exam> .Update;
            var updateDef        = default(UpdateDefinition <Exam>);

            if (examUpdateInfo.Duration.HasValue)
            {
                updateDef = updateDefBuilder.Set(ex => ex.Duration, examUpdateInfo.Duration);
            }

            if (examUpdateInfo.Note != null)
            {
                updateDef = updateDef.Set(ex => ex.Note, examUpdateInfo.Note);
            }

            if (examUpdateInfo.Modifiers != null)
            {
                updateDef = updateDef.Set(ex => ex.Modifiers, examUpdateInfo.Modifiers);
            }

            if (examUpdateInfo.Students != null)
            {
                updateDef = updateDef.Set(ex => ex.Students, examUpdateInfo.Students);
            }

            if (examUpdateInfo.Tests != null)
            {
                updateDef = updateDef.Set(ex => ex.Tests, examUpdateInfo.Tests);
            }

            var updateResult = await this._exams.UpdateOneAsync(ex => ex.Id == exam.Id, updateDef);

            if (updateResult.IsAcknowledged)
            {
                return(UpdateResult.Success);
            }

            return(UpdateResult.Fail);
        }
예제 #2
0
        public async Task <IActionResult> Put([FromBody] ExamUpdateInfo examUpdateInfo)
        {
            var actionResult = default(IActionResult);

            try
            {
                var updateResult = await this._repository.Update(examUpdateInfo,
                                                                 this.GetAuthenticatedUserId());

                switch (updateResult)
                {
                case UpdateResult.NotFound:
                    actionResult = this.NotFound();
                    break;

                case UpdateResult.NoAccess:
                    actionResult = this.Forbid();
                    break;

                case UpdateResult.Success:
                    actionResult = this.Ok();
                    break;

                case UpdateResult.Fail:
                case UpdateResult.ExamEnded:
                    actionResult = this.BadRequest();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception ex)
            {
                this._logger.Log(LogHelper.CreateLog(DateTime.Now, LogType.Fatal, null, ex));

                actionResult = new StatusCodeResult(StatusCodes.Status500InternalServerError);
            }

            return(actionResult);
        }