public UserModel GetUser(int id) { var temp = new UserModel(); User user = db.Users.Find(id); if (user == null) { return null; } else temp = ModelFactory.ParseUserEntityToModel(user); return temp; }
public IHttpActionResult Login(LoginModel model) { var temp = new UserModel(); User user = db.Users.FirstOrDefault(c => c.Email == model.uname && c.Password == model.pass); if (user == null) { return BadRequest("Invalid Credentials"); } else { var token = new TokenModel(user); token.CreateToken(); temp = ModelFactory.ParseUserEntityToModel(user); } return Ok(temp); }
public static UserModel ParseUserEntityToModel(User entity) { UserModel userModelTemp = new UserModel(); userModelTemp.Id = entity.Id; userModelTemp.FirstName = entity.FirstName; userModelTemp.LastName = entity.LastName; userModelTemp.Password = entity.Password; userModelTemp.Email = entity.Email; userModelTemp.Phone = entity.Phone; userModelTemp.DateOfBirth = entity.DateOfBirth.HasValue ? entity.DateOfBirth.Value.ToShortDateString() : null; userModelTemp.City = entity.City; userModelTemp.State = entity.State; userModelTemp.AccountType = entity.AccountType; userModelTemp.Gender = entity.Gender; userModelTemp.AboutMe = entity.AboutMe; userModelTemp.ProfilePicture = entity.ProfilePicture; if(entity.UserToken != null) userModelTemp.Token = entity.UserToken.Token; foreach (var u in entity.Friends) { userModelTemp.Friends.Add(ParseFriendEntityToModel(u)); } foreach (var u in entity.FriendRequest) { userModelTemp.Friends.Add(ParseFriendRequestEntityToModel(u)); } foreach (var w in entity.Post) { userModelTemp.Post.Add(ParseWallEntityToModel(w)); } return userModelTemp; }
public static User ParseUserModelToEntity(UserModel model) { var userEntityTemp = new User(); userEntityTemp.Id = model.Id; userEntityTemp.FirstName = model.FirstName; userEntityTemp.LastName = model.LastName; userEntityTemp.Password = model.Password; userEntityTemp.Email = model.Email; userEntityTemp.Phone = model.Phone; if (!String.IsNullOrEmpty(model.DateOfBirth)) { userEntityTemp.DateOfBirth = Convert.ToDateTime(model.DateOfBirth); } userEntityTemp.City = model.City; userEntityTemp.State = model.State; userEntityTemp.AccountType = model.AccountType; userEntityTemp.Gender = model.Gender; userEntityTemp.AboutMe = model.AboutMe; userEntityTemp.ProfilePicture = model.ProfilePicture; if(model.Token != null) userEntityTemp.UserToken.Token = model.Token; foreach (var u in model.Friends) { userEntityTemp.Friends.Add(ParseFriendModelToEntiy(u)); } foreach (var w in model.Post) { userEntityTemp.Post.Add(ParseWallModelToEntity(w)); } return userEntityTemp; }
public IHttpActionResult PostUser(User user) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (db.Users.Where(c => c.Email == user.Email).Any()) { return BadRequest("Email already exist"); //Ok<string> ("Email already exist"); } db.Users.Add(user); db.SaveChanges(); var tempToken = new UserTokens(); tempToken.User = user; tempToken.Token = ""; db.UserTokens.Add(tempToken); db.SaveChanges(); var tempUser = new UserModel(); tempUser = ModelFactory.ParseUserEntityToModel(user); return CreatedAtRoute("DefaultApi", new { id = tempUser.Id }, tempUser); }