Exemplo n.º 1
0
        private async void AddSubject_Click(object sender, RoutedEventArgs e)
        {
            object @object;

            if (this.VM.IsItemSelected)
            {
                @object = new UpdateSubjectDTO
                {
                    SubjectName = SubjectName.Text,
                    Description = Description.Text,
                    SubjectId   = this.VM.SubjectId
                };
            }
            else
            {
                @object = new AddSubjectDTO
                {
                    SubjectName = SubjectName.Text,
                    Description = Description.Text,
                };
            }


            try
            {
                if (this.VM.IsItemSelected)
                {
                    await AppData.Client.PutAsync(Config.REST_Endpoints_Subjects, @object);
                }
                else
                {
                    await AppData.Client.Post(Config.REST_Endpoints_Subjects, @object);
                }


                await AppData.RefreshSubjectAsync();

                this.VM.Subjects    = AppData.Subjects;
                this.VM.SubjectName = string.Empty;
                this.VM.Description = string.Empty;
            }
            catch (Exception ex)
            {
                string title = $"OH SNAP! Failed to add the following subject : {((Subject)@object).SubjectName}";

                string msg    = $"Couldn't add subject : {((Subject)@object).ToString()}. \n Exception : {ex.Message}. \n Inner Exception : {ex.InnerException?.Message}.";
                var    dialog = new MessageDialog(msg, title);
                await dialog.ShowAsync();

                // Log error
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult> AddSubjectAsync(AddSubjectDTO subject)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var s = _mapper.Map <Subject>(subject);

            try
            {
                await this._service.AddAsync(s);

                await this._service.SaveAsync();
            }
            catch (Exception ex)
            {
                return(Problem(detail: $"Failed to add subject ({s.SubjectName}). Error: {ex.Message}. InnerException: {ex.InnerException?.Message}"));
            }

            return(Ok());
        }
Exemplo n.º 3
0
        //[Authorize(Roles = "AddSubject")]
        public IActionResult Add([FromBody] AddSubjectDTO addSubjectDTO)
        {
            try
            {
                addSubjectDTO.Name = addSubjectDTO.Name.Trim();
                var simelarCode = _subjectRepositroy.Get(c => c.SubjectCode == addSubjectDTO.SubjectCode).FirstOrDefault();
                if (simelarCode != null)
                {
                    var message = Messages.Exist;
                    message.ActionName     = "Add";
                    message.ControllerName = "Subject";
                    return(Conflict(message));
                }

                var subjectType = _subjectTypeRepositroy.Find(addSubjectDTO.SubjectTypeId);
                if (subjectType == null)
                {
                    var message = Messages.NotFound;
                    message.ActionName     = "Add";
                    message.ControllerName = "Subject";
                    message.Message        = "نوع المادة غير موجود";
                    return(Conflict(message));
                }
                var semester = _studySemesterRepositroy.GetIQueryable(c => c.Id == addSubjectDTO.StudySemesterId)
                               .Include(c => c.Studyplan)
                               .ThenInclude(c => c.Year)
                               .FirstOrDefault();
                if (semester == null)
                {
                    var message = Messages.NotFound;
                    message.ActionName     = "Add";
                    message.ControllerName = "Subject";
                    message.Message        = "الفصل التدريسي غير موجود";
                    return(NotFound(message));
                }
                if (semester.Studyplan.Year.Blocked)
                {
                    var message = Messages.NotFound;
                    message.ActionName     = "Add";
                    message.ControllerName = "Subject";
                    message.Message        = "لا يمكن تعديل الخطة ";
                    return(Conflict(message));
                }
                var subject = _mapper.Map <Subjects>(addSubjectDTO);
                _abstractUnitOfWork.Add(subject, UserName());
                subject.StudySemester = semester;
                foreach (var item in addSubjectDTO.DependencySubjectsId)
                {
                    var depencaySubject = _subjectRepositroy.GetIQueryable(c => c.Id == item)
                                          .Include(c => c.StudySemester).FirstOrDefault();
                    var checking = _subjectServices.CheckSubjectAndDepandcySubject(subject, depencaySubject);
                    if (!checking)
                    {
                        return(Conflict());//؟؟؟؟
                    }
                    DependenceSubject dependenceSubject = new DependenceSubject
                    {
                        SubjectId          = subject.Id,
                        DependsOnSubjectId = item
                    };
                    _abstractUnitOfWork.Add(dependenceSubject, UserName());
                }
                foreach (var item in addSubjectDTO.EquivalentSubjectsId)
                {
                    var equivalentSubject = _subjectRepositroy.Find(item);
                    if (equivalentSubject == null)
                    {
                        var message = Messages.NotFound;
                        message.ActionName     = "Add";
                        message.ControllerName = "Subject";
                        message.Message        = "المادةالمكافئة غير موجودة";
                        return(NotFound(message));
                    }
                    EquivalentSubject eq = new EquivalentSubject()
                    {
                        FirstSubject   = subject.Id,
                        SecoundSubject = item
                    };
                    _abstractUnitOfWork.Add(eq, UserName());
                }
                _abstractUnitOfWork.Commit();
                return(Ok(subject));
            }
            catch
            {
                return(BadRequestAnonymousError());
            }
        }