public IEnumerable <PublicUserDTO> GetAllUsersPublic() { return(usersService.GetAllUsers().Select(user => { return DTOConverter.PublicUserDTO(user); })); }
public IHttpActionResult GetUserPublic(int id) { User user = usersService.GetUser(id); if (user == null) { return(NotFound()); } return(Ok(DTOConverter.PublicUserDTO(user))); // this would also work // return Ok(new PublicUserDTO() { Id = user.Id, Name = user.Name }); // another approach is to return DTO directly from the service }
public IHttpActionResult PostRegisterUser(RegisterUserDTO newUser) { if (!ModelState.IsValid) { return(BadRequest("Request body is not in valid format.")); } // If username or email exists new user can not be created // 1. Through DTOConverter // User user = DTOConverter.UserFromDTO(newUser); // usersService.CreateUser(Utilities.DTOConverter.UserDTO(newUser)); // 2. Directly through Service try { User createdUser = usersService.CreateUser(newUser); return(CreatedAtRoute("PrivateUserEndpoint", new { id = createdUser.Id }, DTOConverter.PublicUserDTO(createdUser))); } catch (NameAlreadyExistsException e) { return(BadRequest(e.Message)); } catch (EmailAlreadyExistsException e) { return(BadRequest(e.Message)); } catch (Exception e) { return(InternalServerError(e)); } }