Exemplo n.º 1
0
        public HttpResponseMessage GetHomeworkById(int id)
        {
            try {
                Homeworks hw = this._homeworkManagement.GetHomeworkById(id);

                if (hw != null)
                {
                    HomeworkDTO dto = new HomeworkDTO();
                    dto.HomeworkAccessSpan  = hw.HomeworkAccessSpan;
                    dto.HomeworkDeadline    = hw.HomeworkDeadline;
                    dto.HomeworkDescription = hw.HomeworkDescription;
                    dto.HomeworkId          = hw.HomeworkId;
                    dto.HomeworkName        = hw.HomeworkName;
                    dto.HomeworkPoints      = hw.HomeworkPoints;
                    dto.HomeworkCode        = hw.HomeworkCode;
                    dto.CourseId            = hw.CourseId;
                    dto.CourseModuleId      = hw.CourseModuleId;
                    dto.OwnerId             = hw.OwnerId;

                    return(Request.CreateResponse <HomeworkDTO>(HttpStatusCode.OK, dto));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Resource Not Found"));
                }
            }
            catch (Exception) {
                // Log exception code goes here
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occured while executing method."));
            }
        }
        public async Task CreateAsync(HomeworkDTO item)
        {
            HomeworkValidation(item);
            var homework = _mapper.Map <Homework>(item);
            await _homeworkRepository.CreateAsync(homework);

            await _studentHomeworkUpdater.UpdateAsync(homework,
                                                      StudentHomeworkUpdater.StudentHomeworkUpdater.UpdateType.AddHomework);
        }
        public IActionResult Post([FromBody] HomeworkDTO homeworkDTO)
        {
            try {
                Homework homework        = homeworkDTO.ToEntity();
                Homework createdHomework = homeworksLogic.Create(homework);

                HomeworkDTO homeworkToReturn = new HomeworkDTO(createdHomework);
                return(CreatedAtRoute("Get", new { id = homeworkToReturn.Id }, homeworkToReturn));
            } catch (ArgumentException e) {
                return(BadRequest(e.Message));
            }
        }
        public IActionResult Put(Guid id, [FromBody] HomeworkDTO homeworkDTO)
        {
            try {
                Homework homework        = homeworkDTO.ToEntity();
                Homework updatedHomework = homeworksLogic.Update(id, homework);

                HomeworkDTO homeworkToReturn = new HomeworkDTO(updatedHomework);
                return(CreatedAtRoute("Get", new { id = homework.Id }, updatedHomework));
            } catch (ArgumentException e) {
                return(BadRequest(e.Message));
            }
        }
        public IActionResult Get(Guid id)
        {
            Homework homework = homeworksLogic.Get(id);

            if (homework == null)
            {
                return(NotFound());
            }

            HomeworkDTO homeworkToReturn = new HomeworkDTO(homework);

            return(Ok(homework));
        }
Exemplo n.º 6
0
        public async Task <IHttpActionResult> GetHomework(int id)
        {
            HomeworkDTO dto = await HomeworkService.SearchSingleHomeworkByIdAsync(id);

            if (dto != null)
            {
                return(Ok(dto));
            }
            else
            {
                return(NotFound());
            }
        }
        private void HomeworkValidation(HomeworkDTO homework)
        {
            if (homework.StudentPresence && homework.HomeworkPresence && (homework.Mark < 1 || homework.Mark > 5))
            {
                _logger?.LogWarning($"This mark {homework.Mark} is inappropriate. Must be at least 1 and at most 5");
                throw new ValidationException($"This mark {homework.Mark} is inappropriate. Must be at least 1 and at most 5");
            }

            if ((!homework.StudentPresence || !homework.HomeworkPresence) && homework.Mark > 0)
            {
                _logger?.LogWarning($"This mark {homework.Mark} is inappropriate. Must be 0");
                throw new ValidationException($"This mark {homework.Mark} is inappropriate. Must be 0");
            }
        }
Exemplo n.º 8
0
        private static async Task <HomeworkDTO> GetTest()
        {
            var homework = new HomeworkDTO()
            {
                Id               = 1,
                StudentId        = 1,
                LectureId        = 1,
                StudentPresence  = true,
                HomeworkPresence = true,
                Mark             = 2,
                Date             = DateTime.Now
            };

            return(homework);
        }
Exemplo n.º 9
0
        public async Task <IHttpActionResult> UpdateHomework([FromBody] HomeworkDTO HomeworkModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var updatedHomework = await HomeworkService.UpdateHomeworkAsync(HomeworkModel);

            if (updatedHomework != null)
            {
                return(Ok(updatedHomework));
            }
            else
            {
                return(NotFound());
            }
        }
Exemplo n.º 10
0
        //Update Homework (async)
        public async Task <HomeworkDTO> UpdateHomeworkAsync(HomeworkDTO modelDTO)
        {
            try
            {
                using (var unitOfWork = unitOfWorkFactory.Create())
                {
                    if (!string.IsNullOrEmpty(modelDTO._ImageFileUrl))
                    {
                        var tempImageTypeModel = unitOfWork.ImageFileTypeRepository.GetSingleOrDefaultImageFileType(x => x.Type.Contains("HomeworkFile"));

                        ImageFileTypeDTO imageFileTypeDTO = _Mapper_ToDTO.Map <ImageFileTypeModel, ImageFileTypeDTO>(tempImageTypeModel);

                        modelDTO.ImageFileUrl = new ImageFileUrlDTO()
                        {
                            Url             = modelDTO._ImageFileUrl,
                            CreateDate      = DateTime.Now,
                            ImageFileTypeId = imageFileTypeDTO.ImageFileTypeId
                        };
                    }
                    else
                    {
                        modelDTO.ImageFileUrl = null;
                    }

                    HomeworkModel model = _Mapper_ToModel.Map <HomeworkDTO, HomeworkModel>(modelDTO);

                    bool result = unitOfWork.HomeworkRepository.Update(model);

                    HomeworkDTO modelRTN = null;
                    if (result)
                    {
                        await unitOfWork.SaveChangesAsync();

                        modelRTN = _Mapper_ToDTO.Map <HomeworkModel, HomeworkDTO>(model);
                    }

                    return(modelRTN);
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
                throw ex;
            }
        }
Exemplo n.º 11
0
        public async Task <IHttpActionResult> AddHomework([FromBody] HomeworkDTO HomeworkModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var newHomeworkId = await HomeworkService.AddHomeworkAsync(HomeworkModel);

            if (newHomeworkId != 0)
            {
                return(Ok(newHomeworkId));
            }
            else
            {
                return(NotFound());
            }
        }
Exemplo n.º 12
0
        public async Task UpdateAsync(HomeworkDTO item)
        {
            HomeworkValidation(item);
            var homework = await _homeworkRepository.GetAsync(item.Id);

            var validator = new Validator();

            validator.EntityValidation(homework, _logger, nameof(homework));

            var previousHomeworkPresence = homework.StudentPresence;
            var previousStudentId        = homework.StudentId;

            if (previousStudentId != item.StudentId)
            {
                await _studentHomeworkUpdater.UpdateAsync(homework,
                                                          StudentHomeworkUpdater.StudentHomeworkUpdater.UpdateType.RemoveHomeworkWhileUpdate,
                                                          previousHomeworkPresence);
            }

            homework.StudentId        = item.StudentId;
            homework.LectureId        = item.LectureId;
            homework.StudentPresence  = item.StudentPresence;
            homework.HomeworkPresence = item.HomeworkPresence;
            homework.Mark             = item.Mark;
            homework.Date             = item.Date;
            _homeworkRepository.Update(homework);

            if (previousStudentId != homework.StudentId)
            {
                await _studentHomeworkUpdater.UpdateAsync(homework,
                                                          StudentHomeworkUpdater.StudentHomeworkUpdater.UpdateType.AddHomework,
                                                          previousHomeworkPresence);
            }
            else
            {
                await _studentHomeworkUpdater.UpdateAsync(homework,
                                                          StudentHomeworkUpdater.StudentHomeworkUpdater.UpdateType.UpdateHomework,
                                                          previousHomeworkPresence);
            }
        }
Exemplo n.º 13
0
        public HttpResponseMessage UpdateHomework(int id, HomeworkDTO dto)
        {
            try {
                Homeworks hw = new Homeworks();
                hw.HomeworkAccessSpan  = dto.HomeworkAccessSpan;
                hw.HomeworkDeadline    = dto.HomeworkDeadline;
                hw.HomeworkDescription = dto.HomeworkDescription;
                hw.HomeworkId          = dto.HomeworkId;
                hw.HomeworkName        = dto.HomeworkName;
                hw.HomeworkPoints      = dto.HomeworkPoints;
                hw.HomeworkCode        = dto.HomeworkCode;
                hw.CourseId            = dto.CourseId;
                hw.CourseModuleId      = dto.CourseModuleId;
                hw.OwnerId             = dto.OwnerId;

                this._homeworkManagement.EditHomework(hw);

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception) {
                // Log exception code goes here
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occured while executing method."));
            }
        }