func (place *Place) createPlace(request *restful.Request, response *restful.Response) { place.Lat = 0 place.Lng = 0 place.PostNb = 0 place.ValidatedAt = *new(time.Time) place.Validated = false place.CreateAt = time.Now() place.UserId = place.user.Id place.Id = *new(string) request.Request.ParseMultipartForm(32 << 20) mpf, hdr, _ := request.Request.FormFile("image") ext := filepath.Ext(hdr.Filename) if helpers.CheckFileExtension(ext) == false { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.") response.WriteHeaderAndEntity(406, errors) helpers.PrintLog(request, response, place.user.Name) return } place.Image = helpers.GenerateHash(50) + ext go helpers.PutFile("assets/Images/"+place.Image, mpf) resp, err := r.Table("places").Insert(place).RunWrite(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) return } place.Id = resp.GeneratedKeys[0] response.WriteHeaderAndEntity(http.StatusCreated, place) helpers.PrintLog(request, response, place.user.Name) }
func (message *Message) createMessageWithContent(request *restful.Request, response *restful.Response) { msg := new(Message) msg.Content = message.Content msg.CreatedAt = time.Now() msg.FromUserId = message.user.Id msg.TalkId = message.talk.Id msg.Read = false if message.talk.UserIdX == message.user.Id { msg.ToUserId = message.talk.UserIdY } else { msg.ToUserId = message.talk.UserIdX } resp, err := r.Table("messages").Insert(msg).RunWrite(api.Sess) if err != nil { helpers.PrintLog(request, response, message.user.Name) response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) return } msg.Id = resp.GeneratedKeys[0] message.talk.UpdateLastMessageDate(message.user.Id, message.Content) response.WriteHeaderAndEntity(http.StatusCreated, msg) helpers.PrintLog(request, response, message.user.Name) }
func (match *Match) createMatch(request *restful.Request, response *restful.Response) { newMatch := new(Match) newMatch.CreateAt = time.Now() newMatch.PlaceId = match.post.PlaceId newMatch.PostId = match.post.Id newMatch.UserIdX = match.user.Id newMatch.UserImageX = match.user.UrlPicture newMatch.UserNameX = match.user.Name newMatch.UserIdY = match.post.UserId newMatch.UserNameY = match.post.UserName newMatch.Validated = false resp, err := r.Table("matchs").Insert(newMatch).RunWrite(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) helpers.PrintLog(request, response, match.user.Name) return } go func() { notif := notification.SetMatchReceive() notif.UserId = match.post.UserId notif.UserIdFrom = match.user.Id notif.IdThing = resp.GeneratedKeys[0] notif.Name = match.user.Name notif.IdLink = match.post.Id notif.CreateNotification() }() newMatch.Id = resp.GeneratedKeys[0] response.WriteHeaderAndEntity(http.StatusCreated, newMatch) helpers.PrintLog(request, response, match.user.Name) }
func (user *User) checkBodyCreate(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { private := UserCreate{} errors := api.Error{} err := request.ReadEntity(&private) if err != nil { response.AddHeader("Content-Type", "text/plain") response.WriteErrorString(http.StatusInternalServerError, err.Error()) helpers.PrintLog(request, response, user.Name) return } user.Email = strings.TrimSpace(private.Email) user.Name = helpers.RemoveManySpaces(helpers.MakeFirstUpperCase(strings.ToLower(private.Name))) if user.isTwoPasswordAreIdentical(private.Password, private.Password2) == false { errors.ListErrors = append(errors.ListErrors, "Two passwords aren't identical") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, user.Name) return } if len(private.Password) < 6 { errors.ListErrors = append(errors.ListErrors, "Your password must contain at least 6 characters") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, user.Name) return } user.Password = private.Password chain.ProcessFilter(request, response) }
func (like *Like) createLike(request *restful.Request, response *restful.Response) { newLike := new(Like) newLike.PostId = like.post.Id newLike.UserId = like.user.Id newLike.Created = time.Now() if err := like.post.UpdateLikeNb(1); err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) return } resp, err := r.Table("likes").Insert(newLike).RunWrite(api.Sess) if err != nil { like.post.UpdateLikeNb(-1) response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) helpers.PrintLog(request, response, like.user.Name) return } if like.post.UserId != like.user.Id { go func() { notif := notification.SetLikeReceive() notif.UserId = like.post.UserId notif.UserIdFrom = like.user.Id notif.IdThing = resp.GeneratedKeys[0] notif.Name = like.user.Name notif.IdLink = like.post.Id notif.CreateNotification() }() } newLike.Id = resp.GeneratedKeys[0] response.WriteHeaderAndEntity(http.StatusCreated, newLike) helpers.PrintLog(request, response, like.user.Name) }
func (post *Post) updatePostWithImage(request *restful.Request, response *restful.Response) { request.Request.ParseMultipartForm(32 << 20) mpf, hdr, _ := request.Request.FormFile("image") ext := filepath.Ext(hdr.Filename) if helpers.CheckFileExtension(ext) == false { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.") response.WriteHeaderAndEntity(406, errors) helpers.PrintLog(request, response, post.user.Name) return } if strings.Compare(post.Image, "") != 0 { helpers.RemoveFile("./assets/Images/" + post.Image) } post.Image = helpers.GenerateHash(50) + ext go helpers.PutFile("assets/Images/"+post.Image, mpf) _, err := r.Table("posts").Get(post.Id).Update(post).RunWrite(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) helpers.PrintLog(request, response, post.user.Name) return } response.WriteHeaderAndEntity(http.StatusOK, post) helpers.PrintLog(request, response, post.user.Name) }
func (like *Like) deleteLike(request *restful.Request, response *restful.Response) { like.post.Id = like.PostId if like.post.GetPostById() == false { response.WriteHeader(http.StatusInternalServerError) helpers.PrintLog(request, response, like.user.Name) return } if err := like.post.UpdateLikeNb(-1); err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) helpers.PrintLog(request, response, like.user.Name) return } _, err := r.Table("likes").Get(like.Id).Delete().Run(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) helpers.PrintLog(request, response, like.user.Name) return } go func() { notif := notification.SetLikeReceive() notif.IdThing = like.Id notif.UserIdFrom = like.user.Id notif.RemoveNotification() }() response.WriteHeader(http.StatusOK) helpers.PrintLog(request, response, like.user.Name) }
func (post *Post) acceptPost(request *restful.Request, response *restful.Response) { post.Validated = true post.ValidatedAt = time.Now() if err := post.place.IncrementPostNb(); err != nil { response.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error()) helpers.PrintLog(request, response, post.user.Name) return } post.IdByPlace = post.place.PostNb _, err := r.Table("posts").Get(post.Id).Update(post).RunWrite(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error()) helpers.PrintLog(request, response, post.user.Name) return } go func() { notif := notification.SetPostAccepted() notif.UserId = post.UserId notif.UserIdFrom = post.user.Id notif.IdThing = post.Id notif.Name = post.PlaceName notif.IdLink = post.Id notif.CreateNotification() }() response.WriteHeaderAndEntity(http.StatusOK, post) helpers.PrintLog(request, response, post.user.Name) }
func (post *Post) refusePost(request *restful.Request, response *restful.Response) { _, err := r.Table("posts").Get(post.Id).Delete().Run(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error()) helpers.PrintLog(request, response, post.user.Name) return } if post.Image != "" { helpers.RemoveFile("./assets/Images/" + post.Image) } if post.user.IsUserIsAdmin() == true { go func() { notif := notification.SetPostRefused() notif.UserId = post.UserId notif.UserIdFrom = post.user.Id notif.IdThing = post.Id notif.Name = post.PlaceName notif.IdLink = post.Id notif.CreateNotification() }() } response.WriteHeader(http.StatusOK) helpers.PrintLog(request, response, post.user.Name) }
func (talk *Talk) deleteTalk(request *restful.Request, response *restful.Response) { _, err := r.Table("talks").Get(talk.Id).Delete().Run(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) helpers.PrintLog(request, response, talk.user.Name) return } response.WriteHeader(http.StatusOK) helpers.PrintLog(request, response, talk.user.Name) }
func (post *Post) updatePost(request *restful.Request, response *restful.Response) { _, err := r.Table("posts").Get(post.Id).Update(post).RunWrite(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error()) helpers.PrintLog(request, response, post.user.Name) return } response.WriteHeaderAndEntity(http.StatusOK, post) helpers.PrintLog(request, response, post.user.Name) }
func (user *User) createUserFacebook(request *restful.Request, response *restful.Response) { if user.getUserFacebookWithAccessToken(request.HeaderParameter("facebooktoken")) == false { response.InternalServerError() helpers.PrintLog(request, response, user.Name) return } newUser := new(User) //Facebook's data newUser.FacebookId = user.FacebookId newUser.UrlPicture = user.UrlPicture newUser.Name = user.Name isUserExist := newUser.getUserByFacebookId() if isUserExist == false { //user newUser.Group = 0 newUser.CreateDate = time.Now() newUser.Email = "" newUser.Banish = false } //Session newUser.CreateDateToken = time.Now() newUser.ExpireDateToken = time.Now().AddDate(0, 0, 15) newUser.Token = uuid.New() var resp r.WriteResponse var err error //insert if isUserExist == false { resp, err = r.Table("users").Insert(newUser).RunWrite(api.Sess) //update } else { _, err = r.Table("users").Get(newUser.Id).Update(newUser).RunWrite(api.Sess) } if err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) helpers.PrintLog(request, response, user.Name) return } //if user has been insert, we got ID if isUserExist == false { newUser.Id = resp.GeneratedKeys[0] } response.WriteHeaderAndEntity(http.StatusCreated, newUser) helpers.PrintLog(request, response, user.Name) return }
func (user *User) IsEmailExist(request *restful.Request, response *restful.Response) { user.Email = strings.TrimSpace(request.HeaderParameter("email")) if !helpers.IsAConformEmail(user.Email) { response.WriteHeader(http.StatusNotAcceptable) helpers.PrintLog(request, response, user.Name) return } if user.GetUserByEmail() == false { response.WriteHeader(http.StatusNotFound) } else { response.WriteHeader(http.StatusOK) } helpers.PrintLog(request, response, user.Name) }
func (user *User) createUser(request *restful.Request, response *restful.Response) { newUser := new(User) //user newUser.Email = user.Email newUser.Name = user.Name newUser.CreateDate = time.Now() newUser.Group = 100 newUser.FacebookId = "" newUser.UrlPicture = "" newUser.Banish = false //encrypt password newUser.Password = helpers.NewCryptPasswd([]byte(user.Password)) //session newUser.CreateDateToken = time.Now() newUser.ExpireDateToken = time.Now().AddDate(0, 0, 15) newUser.Token = uuid.New() resp, err := r.Table("users").Insert(newUser).RunWrite(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) helpers.PrintLog(request, response, user.Name) return } newUser.Id = resp.GeneratedKeys[0] newUser.Password = "" response.WriteHeaderAndEntity(http.StatusCreated, newUser) }
func (place *Place) acceptPlace(request *restful.Request, response *restful.Response) { if place.getGeolocation() == false { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "We were unable to geolocate your plate") response.WriteHeaderAndEntity(500, errors) return } place.ValidatedAt = time.Now() place.Validated = true _, err := r.Table("places").Get(place.Id).Update(place).RunWrite(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusConflict, err.Error()) return } go func() { notif := notification.SetPlaceAccepted() notif.UserId = place.UserId notif.UserIdFrom = place.user.Id notif.IdThing = place.Id notif.Name = place.Name notif.IdLink = place.Id notif.CreateNotification() }() response.WriteHeaderAndEntity(http.StatusOK, place) helpers.PrintLog(request, response, place.user.Name) }
func (place *Place) checkDate(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { if place.Type != "event" { chain.ProcessFilter(request, response) return } if place.Type == "event" && place.Recurring == true { chain.ProcessFilter(request, response) return } errors := api.Error{} inOneMonth := time.Now().AddDate(0, 1, 0) lastOneMonth := time.Now().AddDate(0, -1, 0) if place.StartAt.After(place.EndAt) { errors.ListErrors = append(errors.ListErrors, "start date of an event can't be less than end date") } else if place.StartAt.After(inOneMonth) { errors.ListErrors = append(errors.ListErrors, "start date can not be in more than a month in the future") } else if place.EndAt.Before(lastOneMonth) { errors.ListErrors = append(errors.ListErrors, "") } if len(errors.ListErrors) > 0 { response.WriteHeaderAndEntity(http.StatusUnauthorized, errors) helpers.PrintLog(request, response, place.user.Name) return } chain.ProcessFilter(request, response) }
func (match *Match) getMatchs(request *restful.Request, response *restful.Response) { list := ListMatch{} list.List = match.getMatchListByUserId() response.WriteHeaderAndEntity(http.StatusOK, list) helpers.PrintLog(request, response, match.user.Name) }
func (message *Message) checkContent(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { message.Content = helpers.RemoveManySpaces(request.Request.FormValue("content")) if len(message.Content) > 2000 { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "a message must be maximum 2000 characters") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, message.user.Name) return } if len(message.Content) == 0 { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "you're message is empty") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, message.user.Name) return } chain.ProcessFilter(request, response) }
func (match *Match) checkIfUserIsMatchRecipient(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { if match.user.Id != match.UserIdY { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "you must be match's recipient to accept it") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, match.user.Name) return } chain.ProcessFilter(request, response) }
func (match *Match) checkIfMatchIsAccepted(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { if match.Validated == true { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "this match is already validated") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, match.user.Name) return } chain.ProcessFilter(request, response) }
func (post *Post) deletePostImage(request *restful.Request, response *restful.Response) { if post.Image == "" { return } nameImage := post.Image post.Image = "" _, err := r.Table("posts").Get(post.Id).Update(post).RunWrite(api.Sess) if err != nil { response.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error()) helpers.PrintLog(request, response, post.user.Name) return } helpers.RemoveFile("./assets/Images/" + nameImage) response.WriteHeader(http.StatusOK) helpers.PrintLog(request, response, post.user.Name) }
func (user *User) checkEmail(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { errors := api.Error{} //check email's format if !helpers.IsAConformEmail(user.Email) { errors.ListErrors = append(errors.ListErrors, "Email address isn't in right format") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, user.Name) return } if user.GetUserByEmail() == true { errors.ListErrors = append(errors.ListErrors, "Email address is already used") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, user.Name) return } chain.ProcessFilter(request, response) }
func (post *Post) CheckIfPostIsValidated(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { if post.Validated == false { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "This post hasn't been accepted, you cannot have this action on it.") response.WriteHeaderAndEntity(http.StatusNotFound, errors) helpers.PrintLog(request, response, post.user.Name) return } chain.ProcessFilter(request, response) }
func (place *Place) checkIfNameExist(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { if place.getPlaceByName() == true { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "A place or an event related to this name already exists") response.WriteHeaderAndEntity(http.StatusUnauthorized, errors) helpers.PrintLog(request, response, place.user.Name) return } chain.ProcessFilter(request, response) }
func (place *Place) CheckIfPlaceIsValidated(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { if place.Validated == false { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "This spotted isn't validated, you cannot do this action on it.") response.WriteHeaderAndEntity(http.StatusUnauthorized, errors) helpers.PrintLog(request, response, place.user.Name) return } chain.ProcessFilter(request, response) }
func (place *Place) checkType(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { errors := api.Error{} if place.Type != "event" && place.Type != "place" { errors.ListErrors = append(errors.ListErrors, "Type doesn't exist") response.WriteHeaderAndEntity(http.StatusUnauthorized, errors) helpers.PrintLog(request, response, place.user.Name) return } chain.ProcessFilter(request, response) }
func (like *Like) checkIfLikeBelongsUser(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { if like.UserId != like.user.Id { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "This like is not yours") response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors) helpers.PrintLog(request, response, like.user.Name) return } chain.ProcessFilter(request, response) }
func (talk *Talk) checkUserIsInTalk(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { if talk.user.Id != talk.UserIdX && talk.user.Id != talk.UserIdY { errors := api.Error{} errors.ListErrors = append(errors.ListErrors, "This talk doesn't exist.") response.WriteHeaderAndEntity(http.StatusNotFound, errors) helpers.PrintLog(request, response, talk.user.Name) return } chain.ProcessFilter(request, response) }
func (message *Message) getMessages(request *restful.Request, response *restful.Response) { limit, _ := strconv.Atoi(request.HeaderParameter("limit")) offset, _ := strconv.Atoi(request.HeaderParameter("offset")) list := new(MessageList) list.List = message.getListTalksMessages(offset, limit) response.WriteHeaderAndEntity(http.StatusOK, list) helpers.PrintLog(request, response, message.user.Name) }
func (message *Message) checkImage(request *restful.Request, response *restful.Response, chain *restful.FilterChain) { errors := api.Error{} mpf, _, err := request.Request.FormFile("image") if err != nil { errors.ListErrors = append(errors.ListErrors, "We were unable to upload your file") response.WriteHeaderAndEntity(406, errors) helpers.PrintLog(request, response, message.user.Name) return } var buff bytes.Buffer fileSize, _ := buff.ReadFrom(mpf) if fileSize > 300000 { errors.ListErrors = append(errors.ListErrors, "The size of your image is too large") response.WriteHeaderAndEntity(406, errors) helpers.PrintLog(request, response, message.user.Name) return } chain.ProcessFilter(request, response) }