public async Task <object> PostPerson(Person person) { dynamic cResponse = new ExpandoObject(); if (!ModelState.IsValid) { cResponse.Result = "-1"; cResponse.Description = ModelState; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); } int resultEmail = await db.Person.CountAsync(x => x.Email == person.Email); if (resultEmail > 0) { cResponse.Result = "-1"; cResponse.Description = "Email is not available"; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); } int resultUsername = await db.Person.CountAsync(x => x.Username == person.Username); if (resultUsername > 0) { cResponse.Result = "-1"; cResponse.Description = "Username is not available"; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); } person.IsEmailAllowed = true; person.IsPrivate = false; person.IsPushAllowed = true; person.IsSuspended = false; person.LastLoginDate = DateTime.Now; person.PersonRoleID = 1; person.RegisterDate = DateTime.Now; person.GenderID = 3; db.Person.Add(person); await db.SaveChangesAsync(); string tokenResult = BasicHelper.TokenCreate(person.PersonID); if (tokenResult == "-1") { cResponse.Result = "-1"; cResponse.Message = "Token could not created"; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); } cResponse.Result = "0"; cResponse.Token = tokenResult; cResponse.Description = "Person added to database"; cResponse.Person = person; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); }
public async Task <object> PostPerson(Person personLogin) { string email = null; string username = null; if (personLogin.Username.IndexOf("@") == -1) { username = personLogin.Username; } else { email = personLogin.Username; } Person person; if (email != null) { person = await db.Person.Where(x => x.Email == email && x.Password == personLogin.Password).FirstOrDefaultAsync(); } else { person = await db.Person.Where(x => x.Username == username && x.Password == personLogin.Password).FirstOrDefaultAsync(); } dynamic cResponse = new ExpandoObject(); if (person == null) { cResponse.Result = "-1"; cResponse.Message = "Person Not Found"; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); } string tokenResult = BasicHelper.TokenCreate(person.PersonID); if (tokenResult == "-1") { cResponse.Result = "-1"; cResponse.Message = "Token could not created"; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); } person.LastLoginDate = DateTime.Now; await db.SaveChangesAsync(); cResponse.Result = "0"; cResponse.Token = tokenResult; cResponse.Person = person; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); }