document.write("
namespace AndroidApi.Controllers.Api
{
[AllowAnonymous]
[RoutePrefix("v1/account")]
public class AccountController : ApiController
{
private readonly IAccountRepository _repository;
public AccountController(IAccountRepository repository)
{
_repository = repository;
}
[Route("signup")]
public IHttpActionResult Signup(AccountViewModel item)
{
try
{
AccountViewModel result = new AccountViewModel();
if (item == null)
return BadRequest("No user info");
if (_repository.IsEmailUnique(item.Email))
result = _repository.Save(item);
else
return Conflict(); //409
return Ok(result);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
}
}
AccountController.cs - Snippet hosted by \"Cacher\"
namespace AndroidApi.Models.ViewModel
{
public class AccountViewModel : StatusCodeViewModel
{
[JsonProperty("userid")]
public int UserId { get; set; }
[JsonProperty("user")]
public string User { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
[JsonProperty("isactive")]
public bool IsActive { get; set; }
}
}
AccountViewModel.cs - Snippet hosted by \"Cacher\"
namespace AndroidApi.Models.Repository
{
public class AccountRepository : IAccountRepository
{
private static string connectionString;
public AccountRepository()
{
if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["appctxt"]))
connectionString = ConfigurationManager.AppSettings["appctxt"];
}
public bool IsEmailUnique(string email)
{
bool result = true;
DataSet dsResult = new DataSet();
string[] tableNames = new string[] { "CustomerAccessKey" };
SqlParameter[] sqlParameters = new SqlParameter[]
{
new SqlParameter("@email", email)
};
SqlHelper.FillDataset(connectionString, CommandType.StoredProcedure, "UserGetByEmail", dsResult, tableNames, sqlParameters);
if (dsResult.Tables.Count > 0 && dsResult.Tables[0].Rows.Count > 0)
{
result = false;
}
return result;
}
public AccountViewModel Save(AccountViewModel item)
{
AccountViewModel result = new AccountViewModel();
result = Save(item.UserId, item);
return result;
}
public AccountViewModel Save(int id, AccountViewModel item)
{
AccountViewModel result = null;
DataSet dsResult = new DataSet();
string[] tableNames = new string[] { "CustomerAccessKey" };
SqlParameter[] sqlParameters = new SqlParameter[]
{
new SqlParameter("@userId", item.UserId),
new SqlParameter("@username", item.User),
new SqlParameter("@email", item.Email),
new SqlParameter("@password", item.Password),
};
SqlHelper.FillDataset(connectionString, CommandType.StoredProcedure, "UserSave", dsResult, tableNames, sqlParameters);
if (dsResult.Tables.Count > 0 && dsResult.Tables[0].Rows.Count > 0)
{
result = SqlHelper.ToType<AccountViewModel>(dsResult.Tables[0].Rows[0]);
result.StatusCode = 200;
result.Message = "saved";
}
return result;
}
}
}
AccountRepository.cs - Snippet hosted by \"Cacher\"
namespace AndroidApi.Models.Interface
{
public interface IAccountRepository
{
AccountViewModel Save(AccountViewModel item);
AccountViewModel Save(int id, AccountViewModel item);
AccountViewModel GetByEmailAndPassword(AccountViewModel item);
AccountViewModel GetByUserId(int userId);
bool IsEmailUnique(string email);
}
}
AccountInterface.cs - Snippet hosted by \"Cacher\"
public class SignUp extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;
HttpURLConnection connection = null;
BufferedReader reader = null;
Activity activity;
public SignUp(Activity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// display a progress dialog for good user experiance
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Please Wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String result = null;
AccountViewModel account;
Gson gson;
try {
URL url = new URL(params[0]);
String email = params[2];
String password = params[3];
String username = params[1];
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
JSONObject jsonObject = new JSONObject();
jsonObject.put("email", email);
jsonObject.put("password", password);
jsonObject.put("user", username);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();
int statusCode = connection.getResponseCode();
if(statusCode == 200){
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ="";
while ((line = reader.readLine()) != null){
buffer.append(line);
}
String jsonObjectHome = buffer.toString();
result = jsonObjectHome;
}
if (statusCode == 400){
account = new AccountViewModel();
account.setStatusCode(statusCode);
account.setMessage("bad request");
gson = new Gson();
result = gson.toJson(account);
}
if (statusCode == 404){
account = new AccountViewModel();
account.setStatusCode(statusCode);
account.setMessage("User not found please sign up");
gson = new Gson();
result = gson.toJson(account);
}
if (statusCode == 500){
account = new AccountViewModel();
account.setStatusCode(statusCode);
account.setMessage("Internal server error");
gson = new Gson();
result = gson.toJson(account);
}
if (statusCode == 409){
account = new AccountViewModel();
account.setStatusCode(statusCode);
account.setMessage("Email already exists");
gson = new Gson();
result = gson.toJson(account);
}
} catch (Exception e) {
Log.e("MainActivity", e.getMessage(), e);
} finally {
if(connection != null) {
connection.disconnect();
}
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.cancel();
}
}
SignUpAsyncTask.java - Snippet hosted by \"Cacher\"
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_signup, container, false);
try{
InitializeControl(view);
signUp = view.findViewById(R.id.BtnSignUp);
signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (IsValid()){
AccountViewModel account = SignUp(getActivity());
if (account.getStatusCode() == 200){
TokenViewModel token = GetToken(getActivity(), account);
if (token.getStatusCode() == 200){
SaveTokenSession(token);
NavigateToMainActivity();
}else{
Toast messageBox = Toast.makeText(getActivity() , token.getMessage() , Toast.LENGTH_LONG);
messageBox.show();
}
}else {
Toast messageBox = Toast.makeText(getActivity() , account.getMessage() , Toast.LENGTH_LONG);
messageBox.show();
}
}
}
});
}
catch (Exception ex){
String t = ex.getMessage();
}
return view;
}
Signup fragment.java - Snippet hosted by \"Cacher\"
public class AccountViewModel extends StatusCodeViewModel {
private int UserId;
private String Username;
private String Email;
private String Password;
private boolean IsActive;
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public boolean isActive() {
return IsActive;
}
public void setActive(boolean active) {
IsActive = active;
}
}
ViewModel.java - Snippet hosted by \"Cacher\"
");