示例#1
0
        public ActionResult <UserDto> AddUser([FromBody] UserDto newUserToCreate)
        {
            if (string.IsNullOrEmpty(newUserToCreate.Name))
            {
                return(BadRequest(new ProblemDetails()
                {
                    Title = "Bad parameters",
                    Detail = "The name of the new user cannot be empty or null",
                    Instance = HttpContext.Request.Path
                }));
            }

            var userAlreadyExists = _userRepository.Exists(newUserToCreate.Name);

            if (userAlreadyExists)
            {
                return(Conflict(new ProblemDetails()
                {
                    Detail = "User already exists",
                    Title = "Bad parameters",
                    Instance = HttpContext.Request.Path
                }));
            }

            var newUser = new User(newUserToCreate.Name);

            _userRepository.Add(newUser);

            return(Created($"/users/{newUserToCreate.Name}", UserDto.FromModel(newUser)));
        }
示例#2
0
        public ActionResult <UserDto> GetUser(string name)
        {
            var user = _userRepository.Get(name);

            if (user == null)
            {
                return(Problem(
                           "User not found",
                           HttpContext.Request.Path,
                           StatusCodes.Status404NotFound,
                           "Bad parameters"));
            }

            return(Ok(UserDto.FromModel(user)));
        }
        public IEnumerable <UserDto> Get()
        {
            var list    = new List <UserDto>();
            var results = _apiSepContext.Users.ToList();

            if (results.Any())
            {
                foreach (var result in results)
                {
                    list.Add(UserDto.FromModel(result));
                }
            }

            return(list);
        }
        public static CustomerViewModel ConvertToViewModel(Customer dbModel)
        {
            var viewModel = new CustomerViewModel
            {
                Id            = dbModel.Id,
                user          = UserDto.FromModel(dbModel.user),
                country       = CountryDto.FromModel(dbModel.country),
                Address1      = dbModel.Address1,
                Address2      = dbModel.Address2,
                PaymentMethod = dbModel.PaymentMethod,
                Enabled       = dbModel.Enabled,
            };

            return(viewModel);
        }
示例#5
0
 public ActionResult <List <UserDto> > GetAllUsers()
 => Ok(_userRepository
       .GetAll()
       .Select(x => UserDto.FromModel(x)).ToList());
        public ActionResult <UserDto> Get(int id)
        {
            var result = _apiSepContext.Users.FirstOrDefault(p => p.Id == id);

            return(result != null?UserDto.FromModel(result) : null);
        }