public UserResponse Post(User request) { return new UserResponse { Result = Repository.GetOrCreateUser(request), }; }
public User GetOrCreateUser(User user) { if (user.DisplayName.IsNullOrEmpty()) throw new ArgumentNullException("DisplayName"); var userIdAliasKey = "id:User:DisplayName:" + user.DisplayName.ToLower(); using (var redis = RedisManager.GetClient()) { //Get a typed version of redis client that works with <User> var redisUsers = redis.As<User>(); //Find user by DisplayName if exists var userKey = redis.GetValue(userIdAliasKey); if (userKey != null) return redisUsers.GetValue(userKey); //Generate Id for New User if (user.Id == default(long)) user.Id = redisUsers.GetNextSequence(); redisUsers.Store(user); //Save reference to User key using the DisplayName alias redis.SetEntry(userIdAliasKey, user.CreateUrn()); return redisUsers.GetById(user.Id); } }