public async Task <Models.File> SavePublicFileAsync(IFormFile file) { try { var fileInfo = new Models.File() { Id = 0, Name = file.FileName, Path = Config.publicFilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + CodeGenerator.GetCode(10), Size = file.Length }; using (var stream = System.IO.File.Create(fileInfo.Path)) { await file.CopyToAsync(stream); } await dbContext.Files.AddAsync(fileInfo); await dbContext.SaveChangesAsync(); return(fileInfo); } catch (Exception e) { logger.LogError(e.Message); throw new Exception("Server Failed to Save File"); } }
/// <summary> /// 用户注册 /// </summary> /// <param name="user">用户信息</param> /// <returns>注册结果</returns> public async Task RegistAsync(User user) { try { await dbContext.Users.AddAsync(user); await dbContext.SaveChangesAsync(); } catch (Exception e) { logger.LogError(e.Message); throw new Exception("学号已存在"); } }
public async Task AddSystemMessageAsync(string title, string content, string userID) { try { var e = new Event() { EventType = EventType.SystemMessage, RelatedUser = userID, Time = DateTime.Now, Content = JsonConvert.SerializeObject(new { Title = title, Content = content }) }; await dbContext.Events.AddAsync(e); await dbContext.SaveChangesAsync(); } catch (Exception e) { logger.LogError(e.Message); throw new Exception("Action Failed!"); } }
public async Task RemoveCourseTeachersAsync(int courseID) { try { dbContext.UserCourse.RemoveRange(await dbContext.UserCourse.Where(uc => uc.CourseId == courseID).ToArrayAsync()); await dbContext.SaveChangesAsync(); } catch (Exception e) { logger.LogError(e.Message); throw new Exception("Action Failed!"); } }