Exemplo n.º 1
0
        /// <summary>
        /// 添加一个账号
        /// </summary>
        /// <param name="newAccount"></param>
        /// <returns></returns>
        public async Task <Resp> AddAccount(Models.NewAccount newAccount)
        {
            (bool isValid, string msg) = newAccount.IsValid();
            if (!isValid)
            {
                return(Resp.Fault(msg));
            }

            using var db = new MyForContext();

            if (await db.Accounts.AnyAsync(a => a.AccountName == newAccount.AccountName))
            {
                return(Resp.Fault("账号名已经存在"));
            }

            //if (await db.Accounts.AnyAsync(a => a.Email == newAccount.Email))
            int avatarId;

            if (newAccount.Avatar is null)
            {
                avatarId = User.GetDefaultAvatarId();
            }
            else
            {
                Files.File file = new Files.File();
                avatarId = await file.SaveFileToDBAsync(newAccount.Avatar);
            }

            DB.Tables.Account model = new DB.Tables.Account
            {
                AccountName = newAccount.AccountName,
                Password    = newAccount.Password,
                AvatarId    = avatarId,
                Email       = newAccount.Email,
                Phone       = newAccount.Phone,
                Gender      = newAccount.Gender,
                CreateById  = newAccount.CreatorId
            };
            db.Accounts.Add(model);
            int suc = await db.SaveChangesAsync();

            if (suc == 1)
            {
                return(Resp.Success(Resp.NONE, "添加成功"));
            }
            return(Resp.Fault(msg: "添加账号失败"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发布新帖
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <(bool, string)> PulishNewPostAsync(Models.NewPostInfo model)
        {
            bool   suc = true;
            string msg = "";

            /*
             *  先保存文件
             *  会保存在数据库的 Files 表
             *  返回的 fileId 为保存的 ID
             */
            Files.File file = new Files.File();
            if (model.File is null)
            {
                return(false, "需要上传文件");
            }

            int fileId = await file.SaveFileToDBAsync(model.File);

            if (fileId == Files.File.NOT_FILES)
            {
                return(false, "文件上传失败");
            }

            using ATXDbContext db = new ATXDbContext();

            DB.Tables.Post newPost = new DB.Tables.Post
            {
                Author      = model.Author,
                Description = model.Description,
                FileId      = fileId
            };
            db.Posts.Add(newPost);

            //  添加今天的一条发布记录
            await Records.Records.TodayPulishRecordIncreaseAsync();

            if (await db.SaveChangesAsync() == 1)
            {
                return(suc, msg);
            }
            return(false, "发布失败");
        }