public ListResult <LanguageApiModel> List()
 {
     try
     {
         if (CheckUserPermission())
         {
             var langIds = Db.TeacherLangs.Where(c => c.Teacher_UserId == CurrentUser.Id).Select(c => c.LangId).ToList();
             List <LanguageApiModel> langs = new List <LanguageApiModel>();
             foreach (var item in langIds)
             {
                 Language         language   = Db.Languages.Find(item);
                 LanguageApiModel laApiModel = new LanguageApiModel()
                 {
                     Id   = language.Id,
                     Name = language.Name,
                 };
                 langs.Add(laApiModel);
             }
             return(new ListResult <LanguageApiModel>()
             {
                 IsOk = true,
                 Message = new Message("Success, Language Inserted Successfully", MessageType.Success),
                 Items = langs
             });
         }
         else
         {
             return(new ListResult <LanguageApiModel>()
             {
                 IsOk = true,
                 Message = new Message("You must logging in as teacher", MessageType.Error),
                 Items = null
             });
         }
     }
     catch (Exception ex)
     {
         return(new ListResult <LanguageApiModel>()
         {
             IsOk = false,
             Message = new Message(ex.Message + " " + ex.InnerException?.Message, MessageType.Error),
             Items = null
         });
     }
 }
 public Result UpdateLanguage([FromBody] LanguageApiModel model)
 {
     try
     {
         if (CheckUserPermission())
         {
             if (ModelState.IsValid)
             {
                 var lang = Db.Languages.Find(model.Id);
                 if (lang == null)
                 {
                     return(new Result()
                     {
                         IsOk = true,
                         Message = new Message("Language not found", MessageType.Error)
                     });
                 }
                 lang.Name            = model.Name;
                 Db.Entry(lang).State = EntityState.Modified;
                 Db.SaveChanges();
                 return(new Result()
                 {
                     IsOk = true,
                     Message = new Message("Language Updated Successfully", MessageType.Error)
                 });
             }
             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),
         });
     }
 }
 public async Task <Result> RegisterLanguage([FromBody] LanguageApiModel model)
 {
     try
     {
         if (CheckUserPermission())
         {
             if (ModelState.IsValid)
             {
                 var language = Db.Languages.Add(new Language()
                 {
                     Name = model.Name
                 });
                 language.TeacherLangs.Add(new TeacherLang
                 {
                     Teacher_UserId = CurrentUser.Id
                 });
                 Db.Languages.Add(language);
                 Db.SaveChanges();
                 return(new Result()
                 {
                     IsOk = true,
                     Message = new Message("Success, Language 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
         });
     }
 }