Пример #1
0
        public async Task <ResponseReturn> CreateResponse(ResponseToHomework response, string classID, Homework homework)
        {
            try
            {
                _responses = database.GetCollection <ResponseToHomework>(response.homeworkID + "_re");
                _homeworks = database.GetCollection <Homework>(classID + "_ho");
            }
            catch
            {
                return(null);
            }
            await _responses.InsertOneAsync(response);

            if (DateTime.Compare(DateTime.Now, homework.endDate) > 0)
            {
                return(null);
            }

            homework.responses.Add(response.Id);
            var filter = Builders <Homework> .Filter.Eq(x => x.Id, homework.Id);

            await _homeworks.ReplaceOneAsync(filter, homework);

            ResponseReturn responseReturn = new ResponseReturn
            {
                HomeworkObj  = homework,
                ResponseObj  = response,
                HomeworkName = homework.name
            };

            return(responseReturn);
        }
        public async Task <IActionResult> CreateResponse(ResponseToHomeworkDTO responseToHomework)
        {
            string token = HttpContext.Request.Headers["Authorization"];

            token = token.Replace("Bearer ", string.Empty);

            var id   = _tokenHelper.GetIdByToken(token);
            var user = await _apiHelper.ReturnUserByID(id);

            var homework = await _apiHelper.ReturnHomeworkByIDs(responseToHomework.ClassID, responseToHomework.HomeworkID);

            if (homework == null)
            {
                error.Err  = "Złe ID zadania lub klasy";
                error.Desc = "Wprowadź poprawne dane";
                return(StatusCode(405, error));
            }

            ResponseToHomework response = new ResponseToHomework()
            {
                homeworkID    = responseToHomework.HomeworkID,
                senderID      = id,
                senderName    = user.name,
                senderSurname = user.surrname,
                mark          = "",
                description   = responseToHomework.Description,
                homeworkName  = homework.name,
                sendTime      = DateTime.Now,
                files         = responseToHomework.FilesID,
                linkHrefs     = responseToHomework.LinkHrefs
            };
            var responseReturn = await _apiHelper.CreateResponse(response, responseToHomework.ClassID, homework);

            if (responseReturn == null)
            {
                error.Err  = "Nie możesz już oddać zadania";
                error.Desc = "Musisz się pospieszyć na przyszłość";
                return(StatusCode(405, error));
            }
            return(Ok(responseReturn));
        }