public ActionResult <Account> UpdateAccount(Account updatedAccount) { var userId = Convert.ToInt32(User.FindFirst("sub")?.Value); AccountWithTypes existingAccount = accountDAO.GetAccount(userId); Account result = accountDAO.UpdateAccount(updatedAccount); return(Ok(result)); }
public ActionResult <AccountWithTypes> GetAccount() { var userId = Convert.ToInt32(User.FindFirst("sub")?.Value); AccountWithTypes account = accountDAO.GetAccount(userId); if (account != null) { return(Ok(account)); } else { return(NotFound()); } }
//Gets the account based on the user_id. This will show information from the user_favorited_types as well which has all the information about the user's selected types from the questionaire. public AccountWithTypes GetAccount(int user_id) { AccountWithTypes returnAccount = null; try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("SELECT user_account.user_id, user_zip, " + "String_AGG(CONVERT(nvarchar(max), ISNULL(restaurant_type.type, 'N/A')), ', ') AS types, " + "String_AGG(CONVERT(nvarchar(max), ISNULL(restaurant_type.type_id, 'N/A')), ', ') AS typeId FROM user_account " + "JOIN user_favorited_types ON user_favorited_types.user_id = user_account.user_id " + "JOIN restaurant_type ON restaurant_type.type_id = user_favorited_types.type_id " + "WHERE user_account.user_id = @user_id " + "GROUP BY user_account.user_id, user_zip", conn); cmd.Parameters.AddWithValue("@user_id", user_id); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { returnAccount = GetAccountFromReader(reader); } } } } catch (SqlException) { throw; } return(returnAccount); }
private AccountWithTypes GetAccountFromReader(SqlDataReader reader) { string typesString = Convert.ToString(reader["types"]); string typeIdString = Convert.ToString(reader["typeId"]); string[] typeIdStringArray = typeIdString.Split(','); List <int> typeIdList = new List <int>(); for (int i = 0; i < typeIdStringArray.Length; i++) { typeIdList.Add(int.Parse(typeIdStringArray[i])); } int[] typeIdArray = typeIdList.ToArray(); AccountWithTypes a = new AccountWithTypes() { UserId = Convert.ToInt32(reader["user_id"]), ZipCode = Convert.ToInt32(reader["user_zip"]), LikedTypes = typesString.Split(','), LikedTypesId = typeIdArray }; return(a); }