on master help sign in
Merge pull request #131 from ilyakooo0/develop

Removed email from users

commitd6bfc42e2bec182940470897960eb73ab02f4da0 authoriko <ilyakooo0@gmail.com> dateSun 12 May 2019, 12:27 (7 years ago) parente61fa91fc4e27d
3 files changed+57−24
denis/src/Data/Query.hs+22−13 denis/src/Data/User.hs+26−1 denis/src/Server/API.hs+9−10
denis/src/Data/Query.hs+22−13
@@ -58,7 +58,8 @@ module Data.Query (
5858 deactivateToken,
5959 searchUsers,
6060 searchTags,
61− updateUser
61+ updateUser,
62+ UserUpdateRow(..)
6263 ) where
6364
6465import Squeal.PostgreSQL
@@ -287,7 +288,7 @@ getChannelForUserQ = selectStar $
287288 from (table #channels) &
288289 where_ (#namedChannelFullOwner .== param @1 .&& #namedChannelFullId .== param @2)
289290
290−updateUserQ :: Manipulation Schema (TuplePG (User FacultyUrl)) (RowPG (Only UserId))
291+updateUserQ :: Manipulation Schema (TuplePG (UserUpdateRow)) (RowPG (Only UserId))
291292updateUserQ = update #users (
292293 Same `as` #userRowId :*
293294 Set (param @2) `as` #userRowFirstName :*
@@ -296,7 +297,7 @@ updateUserQ = update #users (
296297 Set (param @5) `as` #userRowFacultyUrl :*
297298 Same `as` #userRowEmail :*
298299 Same `as` #userRowIsValidated)
299− (#userRowId .== param @1 .&& #userRowEmail .== param @6)
300+ (#userRowId .== param @1)
300301 (Returning $ #userRowId `as` #fromOnly)
301302
302303getAllChannelsForUserQ :: Query Schema (TuplePG (Only UserId)) (RowPG NamedChannelFull)
@@ -745,9 +746,18 @@ instance ToJSON UserCreation where
745746 ]
746747
747748instance FromJSON UserCreation where
748− parseJSON = withObject "named channel" $ \e ->
749+ parseJSON = withObject "user creation" $ \e ->
749750 UserCreation <$> e .: "firstName" <*> e .: "middleName" <*> e .: "lastName" <*> e .: "faculty" <*> e .: "email"
750751
752+data UserUpdateRow = UserUpdateRow {
753+ userUpdateRowId :: UserId,
754+ userUpdateRowFirstName :: Text,
755+ userUpdateRowMiddleName :: Text,
756+ userUpdateRowLastName :: Text,
757+ userUpdateRowUserFaculty :: FacultyUrl
758+} deriving (GHC.Generic, SOP.Generic, SOP.HasDatatypeInfo)
759+
760+
751761data NamedChannelCreation = NamedChannelCreation {
752762 namedChannelCreationOwner :: UserId,
753763 namedChannelCreationName :: Text,
@@ -1055,19 +1065,18 @@ deactivateToken uId dData = fmap fromOnly <$> (manipulateParams deactivateTokenQ
10551065getVerifiedToken :: UserId -> ByteString -> StaticPQ (Maybe Token)
10561066getVerifiedToken uId tokenData = runQueryParams getVerifiedTokenQ (uId, hash tokenData) >>= firstRow
10571067
1058−updateUser :: (User FacultyUrl) -> StaticPQ (Maybe UserId)
1068+updateUser :: (UserUpdateRow) -> StaticPQ (Maybe UserId)
10591069updateUser user = fmap fromOnly <$> (manipulateParams updateUserQ (normalizeUser user) >>= firstRow)
10601070
10611071-- MARK: Utils
10621072
1063−normalizeUser :: User FacultyUrl -> User FacultyUrl
1064−normalizeUser User{..} = User {
1065− userId = userId,
1066− firstName = T.strip $ firstName,
1067− middleName = T.strip $ middleName,
1068− lastName = T.strip $ lastName,
1069− userFaculty = T.toLower . T.strip $ userFaculty,
1070− userEmail = T.toLower . T.strip $ userEmail
1073+normalizeUser :: UserUpdateRow -> UserUpdateRow
1074+normalizeUser UserUpdateRow{..} = UserUpdateRow {
1075+ userUpdateRowId = userUpdateRowId,
1076+ userUpdateRowFirstName = T.strip $ userUpdateRowFirstName,
1077+ userUpdateRowMiddleName = T.strip $ userUpdateRowMiddleName,
1078+ userUpdateRowLastName = T.strip $ userUpdateRowLastName,
1079+ userUpdateRowUserFaculty = T.toLower . T.strip $ userUpdateRowUserFaculty
10711080}
10721081
10731082data UserRow = UserRow {
denis/src/Data/User.hs+26−1
@@ -11,7 +11,8 @@
1111module Data.User (
1212 User(..),
1313 UserId,
14− UserEmail
14+ UserEmail,
15+ UserUpdate(..)
1516) where
1617
1718import Data.Int (Int64)
@@ -21,12 +22,16 @@ import Data.Text (Text)
2122import Data.Aeson
2223import Servant.Docs (ToSample, toSamples, samples)
2324import Data.Proxy
25+import Data.Faculty
2426
2527-- MARK: Documentation
2628
2729instance (ToSample f) => ToSample (User f) where
2830 toSamples _ = samples $ [User 8 "Vasya" "Pupkinovuch" "Pupkin", User 69 "Seva" "Algebrovich" "Leonidov"] <*> (map snd $ toSamples Proxy) <*> ["foo@hse.ru"]
2931
32+instance ToSample (UserUpdate) where
33+ toSamples _ = samples $ [UserUpdate "Vasya" "Pupkinovuch" "Pupkin", UserUpdate "Seva" "Algebrovich" "Leonidov"] <*> ["cs.hse.ru/dse/"]
34+
3035-- MARK: Implementation
3136
3237type UserId = Int64
@@ -54,3 +59,23 @@ instance (ToJSON f) => ToJSON (User f) where
5459instance (FromJSON f) => FromJSON (User f) where
5560 parseJSON = withObject "named channel" $ \e ->
5661 User <$> e .: "id" <*> e .: "firstName" <*> e .: "middleName" <*> e .: "lastName" <*> e .: "faculty" <*> e .: "email"
62+
63+data UserUpdate = UserUpdate {
64+ userUpdateFirstName :: Text,
65+ userUpdateMiddleName :: Text,
66+ userUpdateLastName :: Text,
67+ userUpdateUserFaculty :: FacultyUrl
68+} deriving (GHC.Generic, SOP.Generic, SOP.HasDatatypeInfo)
69+
70+instance ToJSON UserUpdate where
71+ toJSON (UserUpdate fName mName lName faculty) = object [
72+ "firstName" .= fName,
73+ "middleName" .= mName,
74+ "lastName" .= lName,
75+ "faculty" .= faculty
76+ ]
77+
78+instance FromJSON UserUpdate where
79+ parseJSON = withObject "user creation" $ \e ->
80+ UserUpdate <$> e .: "firstName" <*> e .: "middleName" <*> e .: "lastName" <*> e .: "faculty"
81+
denis/src/Server/API.hs+9−10
@@ -58,7 +58,7 @@ type API =
5858 "users" :> ReqBody '[JSON] [UserId] :> Post '[JSON] [User Faculty] :<|>
5959 "users" :> "all" :> Post '[JSON] [User Faculty] :<|>
6060 "users" :> "search" :> UserSearchDescription :> ReqBody '[JSON, PlainText] Text :> Post '[JSON] [User Faculty] :<|>
61− "users" :> "update" :> UpdateUserDescription :> ReqBody '[JSON] UserCreation :> PostNoContent '[JSON, PlainText] NoContent :<|>
61+ "users" :> "update" :> UpdateUserDescription :> ReqBody '[JSON] UserUpdate :> PostNoContent '[JSON, PlainText] NoContent :<|>
6262 "posts" :> PostApi :<|>
6363 "channels" :> ChannelsApi :<|>
6464 MessagesApi
@@ -85,15 +85,14 @@ mkServerAPI l =
8585 tagsServer :<|>
8686 facultyServer
8787
88−updateUserApi :: UserId -> UserCreation -> App NoContent
89−updateUserApi uId UserCreation{..} = do
90− res <- runQerror $ updateUser User {
91− userId = uId,
92− firstName = userCreationFirstName,
93− middleName = userCreationMiddleName,
94− lastName = userCreationLastName,
95− userFaculty = userCreationUserFaculty,
96− userEmail = userCreationUserEmail
88+updateUserApi :: UserId -> UserUpdate -> App NoContent
89+updateUserApi uId UserUpdate{..} = do
90+ res <- runQerror $ updateUser UserUpdateRow {
91+ userUpdateRowId = uId,
92+ userUpdateRowFirstName = userUpdateFirstName,
93+ userUpdateRowMiddleName = userUpdateMiddleName,
94+ userUpdateRowLastName = userUpdateLastName,
95+ userUpdateRowUserFaculty = userUpdateUserFaculty
9796 }
9897 if isJust res
9998 then return NoContent