func CreateMatch(mr types.MatchRequest) error { var err error err = nil if err = utilsservice.CheckIfUserExists(mr.FirstUserId); err == nil { if err = utilsservice.CheckIfUserExists(mr.SecondUserId); err == nil { var firstUsername string var secondUsername string if firstUsername, err = utilsservice.GetUserUsername(mr.FirstUserId); err == nil { if secondUsername, err = utilsservice.GetUserUsername(mr.SecondUserId); err == nil { var m types.Match m.UserId = uuid.Parse(mr.FirstUserId) m.MatchedUserId = uuid.Parse(mr.SecondUserId) m.MatchedUsername = secondUsername m.Timestamp = int64(time.Now().UTC().Unix()) //create first match if err = chatRepo.CreateMatch(m); err == nil { m.UserId = uuid.Parse(mr.SecondUserId) m.MatchedUserId = uuid.Parse(mr.FirstUserId) m.MatchedUsername = firstUsername m.Timestamp = int64(time.Now().UTC().Unix()) //create second match if err = chatRepo.CreateMatch(m); err == nil { var cr types.ChatRequest //create chat cr.FirstUserId = mr.FirstUserId cr.SecondUserId = mr.SecondUserId err = CreateChat(cr) //send push message to first user if lastActivity, errA := activityservice.GetUserActivity(mr.FirstUserId); errA == nil { notificationsservice.SendPushNotification(lastActivity.DeviceType, lastActivity.PushToken, "you've been matched!") } //send push message to second user if lastActivity, errA := activityservice.GetUserActivity(mr.SecondUserId); errA == nil { notificationsservice.SendPushNotification(lastActivity.DeviceType, lastActivity.PushToken, "you've been matched!") } } } } } } } return err }
func (repo *CassandraMatchRepo) GetUserMatch(userId string) (types.Match, error) { // get match for user by id log.Printf(fmt.Sprintf("CassandraMatchRepo.GetUserMatch() - Received userId: %v", userId)) var match types.Match var user_id string var matched_user_id string var err error err = nil sql := fmt.Sprintf(`SELECT user_id, matched_user_id, matched_username, timestamp FROM matches WHERE user_id = %v LIMIT 1`, userId) log.Printf(sql) if err = session.Query(sql).Scan(&user_id, &matched_user_id, &match.MatchedUsername, &match.Timestamp); err != nil { log.Printf(fmt.Sprintf("CassandraMatchRepo.GetUserMatch() - Error: %v", err.Error())) } else { match.UserId = uuid.Parse(user_id) match.MatchedUserId = uuid.Parse(matched_user_id) } return match, err }