コード例 #1
0
        public async Task <UserChatSettings> Get(GetMySettings request)
        {
            var session = Request.ThrowIfUnauthorized();

            var settingsId = session?.UserAuthId + "/settings";

            var typeMessage = await RavenSession.Query <Settings>().FirstOrDefaultAsync(x => x.Id == settingsId);

            if (typeMessage != null)
            {
                var settings = Mapper.Map <UserChatSettings>(typeMessage);
                return(settings);
            }
            else
            {
                var settings = new Settings {
                    SendingMessageByEnterKey = true, Id = settingsId
                };

                await RavenSession.StoreAsync(settings);

                await RavenSession.SaveChangesAsync();

                var settingsTypeMessage = Mapper.Map <UserChatSettings>(settings);
                return(settingsTypeMessage);
            }
        }
コード例 #2
0
        private async Task <User> CreateUser(string login, string userName, string password = null)
        {
            var uid  = $"{UserPrefix}{Guid.NewGuid()}";
            var user = new User
            {
                Id             = uid,
                Login          = login,
                DisplayName    = String.IsNullOrWhiteSpace(userName)? null: userName,
                RegisteredTime = DateTimeOffset.UtcNow,
            };
            await RavenSession.StoreAsync(user);

            if (password != null)
            {
                byte[] salt = Hashing.CreateSalt();

                var secret = new UserSecret
                {
                    Id       = uid + SecretPostfix,
                    Salt     = salt,
                    Password = Hashing.CreateHashPassword(password, salt),
                };
                await RavenSession.StoreAsync(secret);
            }

            await RavenSession.SaveChangesAsync();

            return(user);
        }
コード例 #3
0
        public async Task <PasswordChangeResult> Post(CreatePassword request)
        {
            var session = Request.ThrowIfUnauthorized();

            var uid = session?.UserAuthId;

            if (uid == null)
            {
                throw new HttpError(HttpStatusCode.Unauthorized);
            }

            var user = await GetUserById(uid);

            if (user == null)
            {
                throw new HttpError(HttpStatusCode.NotFound);
            }

            var secret = await GetUserSecret(uid);

            if (secret.Password != null)
            {
                throw new HttpError(HttpStatusCode.Conflict);
            }

            secret.Password = request.NewPassword;

            await RavenSession.StoreAsync(secret);

            await RavenSession.SaveChangesAsync();

            return(new PasswordChangeResult {
                Result = PasswordChangeResult.ChangeEnum.Created
            });
        }
コード例 #4
0
        public async Task <AttachmentMold> Post(SetAttachment request)
        {
            var session = Request.ThrowIfUnauthorized();
            var file    = Request.Files.FirstOrDefault();

            if (file == null)
            {
                throw HttpError.BadRequest("Error: No file to download");
            }

            var stream = file?.InputStream;
            var fileId = $"{pref}{Guid.NewGuid()}";

            var fileUpload = new Attachment()
            {
                Id             = fileId,
                FileName       = file?.FileName,
                SenderId       = session.UserAuthId,
                Hash           = SaveFile(fileId.Replace(pref, string.Empty), stream),
                UploadDateTime = DateTimeOffset.Now,
                Size           = stream.Length
            };

            await RavenSession.StoreAsync(fileUpload);

            await RavenSession.SaveChangesAsync();

            var mapped = Mapper.Map <AttachmentMold>(fileUpload);

            return(mapped);
        }
コード例 #5
0
        public async Task <UserChatSettings> Post(SetSettings request)
        {
            var session    = Request.ThrowIfUnauthorized();
            var settingsId = session?.UserAuthId + "/settings";

            var settings = Mapper.Map <Settings>(request);

            settings.Id = settingsId;

            await RavenSession.StoreAsync(settings);

            await RavenSession.SaveChangesAsync();

            var mapped = Mapper.Map <UserChatSettings>(settings);

            return(mapped);
        }
コード例 #6
0
ファイル: ProfileService.cs プロジェクト: SkillChat/SkillChat
        public async Task <UserProfileMold> Post(SetProfile request)
        {
            var session = Request.ThrowIfUnauthorized();
            var uid     = session?.UserAuthId;

            var existedUser = await RavenSession.Query <User>().FirstOrDefaultAsync(x => x.Id == uid);

            if (existedUser != null)
            {
                request.DisplayName = new string(request.DisplayName.Take(32).ToArray());

                var user = Mapper.Map <SetProfile, User>(request, existedUser);

                await RavenSession.StoreAsync(user);

                await RavenSession.SaveChangesAsync();

                var mapped = Mapper.Map <UserProfileMold>(user);
                return(mapped);
            }
            throw new HttpError(HttpStatusCode.NotFound, "Пользователь не найден в базе данных");
        }