public HttpResponseMessage BreakOffFriendship([FromBody] IdModel model)
        {
            if (ValidationService.AuthorizeToken(GetToken(), "post:/api/volunteer/breakofffriendship") == false)
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Unauthorized, Content = new StringContent("无访问权限", System.Text.Encoding.GetEncoding("UTF-8"), "application/text")
                });
            }
            Guid otherVolunteerId = new Guid(model.id);
            User myself           = ValidationService.FindUserWithToken(GetToken());
            User other            = myService.FindUser(otherVolunteerId);
            Guid myId             = myself.Id;

            if (FriendService.CheckIfWeAreFriends(myId, otherVolunteerId) == false)
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Forbidden, Content = new StringContent("你们不是好友,无法断绝关系", System.Text.Encoding.GetEncoding("UTF-8"), "application/text")
                });
            }
            if (myService.FriendServiceInVolunteerService.BreakOffFriendship(myself, other) == false)
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Forbidden, Content = new StringContent("断绝好友关系不成功", System.Text.Encoding.GetEncoding("UTF-8"), "application/text")
                });
            }
            else
            {
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
        }
        public HttpResponseMessage AcceptFriend([FromBody] IdAndCommentModel model)
        {
            if (ValidationService.AuthorizeToken(GetToken(), "post:/api/volunteer/acceptfriend") == false)
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Unauthorized, Content = new StringContent("无访问权限", System.Text.Encoding.GetEncoding("UTF-8"), "application/text")
                });
            }
            Guid otherVolunteerId = new Guid(model.id);
            User myself           = ValidationService.FindUserWithToken(GetToken());
            User other            = myService.FindUser(otherVolunteerId);
            Guid myId             = myself.Id;

            //检查是否已经是好友
            if (FriendService.CheckIfWeAreFriends(myId, otherVolunteerId) == true)
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Forbidden, Content = new StringContent("你们已经是好友了", System.Text.Encoding.GetEncoding("UTF-8"), "application/text")
                });
            }
            //同意好友申请并添加好友
            if (myService.FriendServiceInVolunteerService.AcceptFriendApplication(other, myself, model.comment) == false)
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Forbidden, Content = new StringContent("同意好友申请不成功", System.Text.Encoding.GetEncoding("UTF-8"), "application/text")
                });
            }
            else
            {
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
        }
Ejemplo n.º 3
0
        public HttpResponseMessage GetUserBadges(string id, string sortByKey, bool isAscending, int pageIndex, int pageSize)
        {
            if (ValidationService.AuthorizeToken(GetToken(), "get:/api/badge/userbadges?id=&sortByKey=&isAscending=&pageIndex=&pageSize=") == false)
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Unauthorized, Content = new StringContent("无访问权限", System.Text.Encoding.GetEncoding("UTF-8"), "application/text")
                });
            }
            Guid userId      = new Guid(id);
            User user        = myService.FindUser(userId);
            User currentUser = ValidationService.FindUserWithToken(GetToken());

            //如果当前用户和user都是volunteer,必须是自己或者好友才能调用该web api看到badge
            if (user.UserRole.Contains(Role.Volunteer) && currentUser.UserRole.Contains(Role.Volunteer))
            {
                if (currentUser.Id != userId)
                {
                    if (FriendService.CheckIfWeAreFriends(currentUser.Id, userId) == false)
                    {
                        return(new HttpResponseMessage {
                            StatusCode = HttpStatusCode.Forbidden, Content = new StringContent("无访问权限", System.Text.Encoding.GetEncoding("UTF-8"), "application/text")
                        });
                    }
                }
            }
            List <BadgeEntity> source     = BadgeService.FindAllUserGrantedBadgeEntity(userId, sortByKey, isAscending, pageIndex, pageSize);
            var            result         = transformBadgeEntityToListShow(source);
            StringWriter   tw             = new StringWriter();
            JsonSerializer jsonSerializer = new JsonSerializer();

            jsonSerializer.Serialize(tw, result, result.GetType());
            return(new HttpResponseMessage {
                Content = new StringContent(tw.ToString(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }