Ejemplo n.º 1
0
        public async Task <ObjectResult> Delete(string classId)
        {
            Guid _classId;

            try
            {
                if (!Guid.TryParse(classId, out _classId))
                {
                    return(BadRequest(classId));
                }

                var response = await _classProvider.Delete(classId);

                if (response.Success)
                {
                    List <Task> deleteTasks = new List <Task>(), updateIndexTasks = new List <Task>();
                    var         retrieveUpdatedTasks = new List <Task <IProviderResponse <IStudent> > >();

                    /*
                     * await _firehoseProvider.PublishAsync(Topics.Logs | Topics.SearchIndexes, new Activity<IClass>()
                     *  {
                     *      Id = Guid.NewGuid().ToString(),
                     *      ActivityType = ActivityType.ClassDelete,
                     *      Timestamp = DateTime.UtcNow,
                     *      Payload = new ActivityPayload<IClass>(){
                     *          Data = response.Data
                     *      }
                     *  }
                     * );
                     */

                    updateIndexTasks.Add(_searchProvider.RemoveFromElastic(response.Data));

                    foreach (var studentId in response.Data.EnrolleeIds)
                    {
                        deleteTasks.Add(_studentProvider.UnEnroll(response.Data.Code, studentId));
                    }
                    await Task.WhenAll(deleteTasks);

                    foreach (var studentId in response.Data.EnrolleeIds)
                    {
                        retrieveUpdatedTasks.Add(_studentProvider.Retrieve(studentId));
                    }
                    await Task.WhenAll(retrieveUpdatedTasks);

                    foreach (var completedTask in retrieveUpdatedTasks)
                    {
                        if (completedTask.Result.Success)
                        {
                            var updatedStudent = completedTask.Result.Data;
                            updateIndexTasks.Add(_searchProvider.UpdateElastic(updatedStudent));
                        }
                    }

                    await Task.WhenAll(updateIndexTasks);

                    return(StatusCode((int)HttpStatusCode.OK, response));
                }
                else
                {
                    /*
                     * await _firehoseProvider.PublishAsync(Topics.Logs, new Activity<string>(){ Id = Guid.NewGuid().ToString(),
                     *                                                                    Timestamp = DateTime.UtcNow,
                     *                                                                    Payload = new ActivityPayload<string>()
                     *                                                                              { Data = string.Format($"ClassesController: Edit(), RequestJson: {classUpdateRequestJson}, ErrorResponse: {response}")},
                     *                                                                    ActivityType = ActivityType.Log});
                     */
                    return(StatusCode((int)HttpStatusCode.InternalServerError, response));
                }
            }
            catch (Exception ex)
            {
                /*
                 * await _firehoseProvider.PublishAsync(Topics.Logs, new Activity<string>(){ Id = Guid.NewGuid().ToString(),
                 *                                                                        Timestamp = DateTime.UtcNow,
                 *                                                                        Payload = new ActivityPayload<string>()
                 *                                                                                  { Data = string.Format($"ClassesController: Delete(), ClassId: {classId}, Exception: {ex}")},
                 *                                                                        ActivityType = ActivityType.Log});
                 */
                return(StatusCode((int)HttpStatusCode.InternalServerError, new ProviderResponse <IClass>()
                {
                    Success = false, ResponseStatus = CustomResponseErrors.ClassUpdateFailed.ToString("f"),
                    Exception = ex, Message = ex.Message
                }));
            }
        }
Ejemplo n.º 2
0
        public async Task <ObjectResult> UnEnroll(string studentId, string classCode)
        {
            Guid _studentId, _classCode;
            var  unenrollTasks    = new List <Task <IProviderResponse <bool> > >();
            var  updateIndexTasks = new List <Task>();

            try
            {
                if (!Guid.TryParse(studentId, out _studentId))
                {
                    return(BadRequest(studentId));
                }

                if (!Guid.TryParse(classCode, out _classCode))
                {
                    return(BadRequest(classCode));
                }

                var studentUnenrollTask = _studentProvider.UnEnroll(classCode, studentId);
                unenrollTasks.Add(studentUnenrollTask);
                unenrollTasks.Add(_classProvider.UnEnroll(classCode, studentId));

                await Task.WhenAll(unenrollTasks);

                var updatedStudentResponseTask = _studentProvider.Retrieve(studentId);
                var updatedClassResponseTask   = _classProvider.Retrieve(classCode);

                /*
                 *  await _firehoseProvider.PublishAsync(Topics.Logs | Topics.SearchIndexes, new Activity<IStudent>()
                 *      {
                 *          Id = Guid.NewGuid().ToString(),
                 *          ActivityType = ActivityType.Unenroll,
                 *          Timestamp = DateTime.UtcNow,
                 *          Payload = new ActivityPayload<IStudent>(){
                 *              Data = updatedStudentResponse.Data
                 *          }
                 *      }
                 *  );
                 */
                await Task.WhenAll(updatedStudentResponseTask, updatedClassResponseTask);

                updateIndexTasks.AddRange(new Task[] { _searchProvider.UpdateElastic(updatedStudentResponseTask.Result.Data),
                                                       _searchProvider.UpdateElastic(updatedClassResponseTask.Result.Data) });
                await Task.WhenAll(updateIndexTasks);

                return((!unenrollTasks.Any(p => !p.Result.Success)) ? StatusCode((int)HttpStatusCode.OK, studentUnenrollTask.Result) : StatusCode((int)HttpStatusCode.InternalServerError, studentUnenrollTask.Result));
            }
            catch (Exception ex)
            {
                /*
                 * await _firehoseProvider.PublishAsync(Topics.Logs, new Activity<string>(){ Id = Guid.NewGuid().ToString(),
                 *                                                                        Timestamp = DateTime.UtcNow,
                 *                                                                        Payload = new ActivityPayload<string>()
                 *                                                                                  { Data = string.Format($"StudentsController: UnEnroll(), StudentId: {studentId}, ClassCode: {classCode}, Error: {ex}")},
                 *                                                                        ActivityType = ActivityType.Log});
                 */
                return(StatusCode((int)HttpStatusCode.InternalServerError, new ProviderResponse <IClass>()
                {
                    Success = false, ResponseStatus = CustomResponseErrors.StudentUpdateFailed.ToString("f"),
                    Exception = ex, Message = ex.Message
                }));
            }
        }