Esempio n. 1
0
        public virtual async Task <IActionResult> UpdateAsync([FromBody] SubjectApiModel subject)
        {
            if (subject == null)
            {
                return(new JsonResult(null));
            }

            SubjectApiModel model = await this._service.UpdateAsync(subject).ConfigureAwait(false);

            return(new JsonResult(model));
        }
Esempio n. 2
0
        public virtual async Task <IActionResult> DeleteAsync(Int64 id)
        {
            if (id < 0)
            {
                return(null);
            }

            SubjectApiModel model = await this._service.DeleteAsync(id).ConfigureAwait(false);

            return(new JsonResult(model));
        }
Esempio n. 3
0
 public ListResult <SubjectApiModel> List()
 {
     try
     {
         if (CheckUserPermission())
         {
             var subjIds = Db.TeacherSubjs.Where(c => c.Teacher_UserId == CurrentUser.Id).Select(c => c.SubjectId).ToList();
             List <SubjectApiModel> subjects = new List <SubjectApiModel>();
             foreach (var item in subjIds)
             {
                 Subject         subject    = Db.Subjects.Find(item);
                 SubjectApiModel suApiModel = new SubjectApiModel()
                 {
                     Id   = subject.Id,
                     Name = subject.Name,
                 };
                 subjects.Add(suApiModel);
             }
             return(new ListResult <SubjectApiModel>()
             {
                 IsOk = true,
                 Message = new Message("Success, Subjects Returned Successfully ", MessageType.Success),
                 Items = subjects
             });
         }
         else
         {
             return(new ListResult <SubjectApiModel>()
             {
                 IsOk = true,
                 Message = new Message("You must logging in as teacher", MessageType.Error),
                 Items = null
             });
         }
     }
     catch (Exception ex)
     {
         return(new ListResult <SubjectApiModel>()
         {
             IsOk = false,
             Message = new Message(ex.Message + " " + ex.InnerException?.Message, MessageType.Error),
             Items = null
         });
     }
 }
Esempio n. 4
0
        public async Task <IActionResult> Get(Int64 id)
        {
            SubjectApiModel model = await this._service.GetByIdAsync(id).ConfigureAwait(false);

            return(new JsonResult(model));
        }
Esempio n. 5
0
 public Result UpdateSubject([FromBody] SubjectApiModel model)
 {
     try
     {
         if (CheckUserPermission())
         {
             if (ModelState.IsValid)
             {
                 var subject = Db.Subjects.Find(model.Id);
                 if (subject == null)
                 {
                     return(new Result()
                     {
                         IsOk = true,
                         Message = new Message("Subject not found", MessageType.Error)
                     });
                 }
                 subject.Name            = model.Name;
                 Db.Entry(subject).State = EntityState.Modified;
                 Db.SaveChanges();
                 return(new Result()
                 {
                     IsOk = true,
                     Message = new Message("Subject Updated Successfully", MessageType.Success)
                 });
             }
             else
             {
                 List <string> errors = new List <string>();
                 foreach (var item in ModelState.Values)
                 {
                     foreach (var error in item.Errors)
                     {
                         errors.Add(error.ErrorMessage);
                     }
                 }
                 return(new Result()
                 {
                     IsOk = true,
                     Message = new Message(errors.ToString(), MessageType.Error),
                 });
             }
         }
         else
         {
             return(new Result()
             {
                 IsOk = true,
                 Message = new Message("You must logging in as teacher", MessageType.Error),
             });
         }
     }
     catch (Exception ex)
     {
         return(new Result()
         {
             IsOk = false,
             Message = new Message(ex.Message + " " + ex.InnerException?.Message, MessageType.Error),
         });
     }
 }
Esempio n. 6
0
 public Result RegisterSubject([FromBody] SubjectApiModel model)
 {
     try
     {
         if (CheckUserPermission())
         {
             if (ModelState.IsValid && CurrentUser != null)
             {
                 var subject = Db.Subjects.Add(new Subject()
                 {
                     Name = model.Name
                 });
                 Db.SaveChanges();
                 Db.TeacherSubjs.Add(new TeacherSubj
                 {
                     SubjectId      = subject.Id,
                     Teacher_UserId = CurrentUser.Id
                 });
                 Db.SaveChanges();
                 return(new Result()
                 {
                     IsOk = true,
                     Message = new Message("Success, Subject Inserted Successfully", MessageType.Success)
                 });
             }
             else
             {
                 List <string> errors = new List <string>();
                 foreach (var item in ModelState.Values)
                 {
                     foreach (var error in item.Errors)
                     {
                         errors.Add(error.ErrorMessage);
                     }
                 }
                 return(new Result()
                 {
                     IsOk = true,
                     Message = new Message(errors.ToString(), MessageType.Error),
                 });
             }
         }
         else
         {
             return(new Result()
             {
                 IsOk = true,
                 Message = new Message("You must logging in as teacher", MessageType.Error),
             });
         }
     }
     catch (Exception ex)
     {
         return(new LoginResult()
         {
             IsOk = false,
             Message = new Message(ex.Message + " " + ex.InnerException?.Message, MessageType.Error),
             ResultCode = LoginEnumResult.Failure,
             ResultText = LoginEnumResult.Failure.ToString(),
             Token = null
         });
     }
 }
Esempio n. 7
0
 public ListResult <SubjectApiModel> List(string keyword = null)
 {
     try
     {
         if (!CheckUserPermission())
         {
             return new ListResult <SubjectApiModel>()
                    {
                        IsOk    = true,
                        Message = new Message("Error ,Please Login as Student", MessageType.Error),
                        Items   = null
                    }
         }
         ;
         var subjs = Db.Subjects
                     .Include(cc => cc.TeacherSubjs)
                     .ToList();
         if (!string.IsNullOrEmpty(keyword))
         {
             subjs = subjs.Where(c => c.Name.ToLower().Contains(keyword.ToLower())).ToList();
         }
         List <SubjectApiModel> subjects = new List <SubjectApiModel>();
         foreach (var item in subjs)
         {
             List <TeacherApiModel> subjectTeachers = new List <TeacherApiModel>();
             foreach (var teacher in item.TeacherSubjs)
             {
                 Teacher te = Db.Teachers.Find(teacher.Teacher_UserId);
                 subjectTeachers.Add(new TeacherApiModel {
                     Id       = te.Id,
                     FirsName = te.FirstName,
                     LastName = te.LastName,
                     Role     = Discriminator.Teacher.ToString(),
                     UserId   = te.UserId
                 });
             }
             SubjectApiModel suApiModel = new SubjectApiModel()
             {
                 Id       = item.Id,
                 Name     = item.Name,
                 Teachers = subjectTeachers
             };
             subjects.Add(suApiModel);
         }
         return(new ListResult <SubjectApiModel>()
         {
             IsOk = true,
             Message = new Message("Success, Subjects Found Successfully", MessageType.Success),
             Items = subjects
         });
     }
     catch (Exception ex)
     {
         return(new ListResult <SubjectApiModel>()
         {
             IsOk = false,
             Message = new Message(ex.Message + " " + ex.InnerException?.Message, MessageType.Error),
             Items = null
         });
     }
 }