コード例 #1
0
        public async Task <ActionResult <UserDto> > Post([FromBody] UserDto request)
        {
            if (string.IsNullOrEmpty(request.Login) ||
                string.IsNullOrEmpty(request.Password) ||
                string.IsNullOrEmpty(request.FirstName) ||
                string.IsNullOrEmpty(request.LastName))
            {
                return(BadRequest(Errors.REQUIRED_FIELDS_ARE_MISSING));
            }

            try
            {
                var user = await _ethereumUserService.AddAsyncCall(
                    request.Login, request.Password, request.FirstName, request.LastName, request.Info);

                return(Ok(ConvertToDto(user)));
            }
            catch (Exception e)
            {
                if (e.Message.Contains("LOGIN ALREADY EXISTS"))
                {
                    return(BadRequest(Errors.LOGIN_ALREADY_EXISTS));
                }

                return(StatusCode(500, new ErrorDto($"{e.Message}\n{e.StackTrace}")));
            }
        }
コード例 #2
0
        public async Task <ActionResult <UserDto> > Put([FromBody] UserDto request)
        {
            var login    = Request.Headers["X-Login"];
            var password = Request.Headers["X-Token"];

            if ((string.IsNullOrEmpty(request.FirstName) ||
                 string.IsNullOrEmpty(request.LastName)) &&
                string.IsNullOrEmpty(request.Info))
            {
                return(BadRequest(Errors.REQUIRED_FIELDS_ARE_MISSING));
            }

            var auth = await _ethereumUserService.IsAuthenticatedAsyncCall(login, password);

            if (!auth)
            {
                return(BadRequest(Errors.WRONG_CREDENTIALS));
            }

            try
            {
                if (!string.IsNullOrEmpty(request.FirstName) && !string.IsNullOrEmpty(request.LastName))
                {
                    await _ethereumUserService.SetNameAsync(
                        login, password, request.FirstName, request.LastName, DateTime.Now);
                }

                if (!string.IsNullOrEmpty(request.Info))
                {
                    await _ethereumUserService.SetInfoAsync(login, password,
                                                            request.Info, DateTime.Now);
                }

                var user = await _ethereumUserService.GetAsyncCall(login);

                return(Ok(ConvertToDto(user)));
            }
            catch (Exception e)
            {
                if (e.Message.Contains("LOGIN DOESN'T EXIST"))
                {
                    return(BadRequest(Errors.WRONG_CREDENTIALS));
                }

                if (e.Message.Contains("WRONG CREDENTIALS"))
                {
                    return(BadRequest(Errors.WRONG_CREDENTIALS));
                }

                //only owner can edit
                if (e.Message.Contains("INSUFFICIENT PRIVILEGES"))
                {
                    return(StatusCode(403, Errors.INSUFFICIENT_PRIVILEGES));
                }

                return(StatusCode(500, new ErrorDto(e.Message)));
            }
        }