Exemple #1
0
        public IHttpActionResult GetUser(int id)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);
            var tokenId    = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);

            if ((permission & 1) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            if (tokenId != id)
            {
                return(BadRequest("使用者錯誤"));
            }
            var user = _db.Users.Find(id);

            return(Ok(new
            {
                user.Id,
                user.Account,
                user.Nickname,
                user.Name,
                user.Picture,
                user.Email,
                user.Phone,
                user.Birthday,
                user.BuyerAverageStar,
                user.SellerAverageStar
            }));
        }
        public IHttpActionResult GetRoomUser()
        {
            var tokenId   = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);
            var roomUsers = _db.Rooms.Where(room => room.SellerId == tokenId);

            return(Ok(roomUsers.Select(room => new
            {
                room.Id,
            })));
        }
        public IHttpActionResult PutRoom(int id, [FromBody] Room newRoom)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);

            if ((permission & 4) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var room = _db.Rooms.Find(id);

            if (room == null)
            {
                return(NotFound());
            }
            var tokenId = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);

            if (tokenId != room.SellerId)
            {
                return(BadRequest("使用者錯誤"));
            }
            room.CountryId        = newRoom.CountryId == 0 ? room.CountryId : newRoom.CountryId;
            room.CountyId         = newRoom.CountyId == 0 ? room.CountyId : newRoom.CountyId;
            room.CityId           = newRoom.CityId == 0 ? room.CityId : newRoom.CityId;
            room.TagId            = newRoom.TagId == 0 ? room.TagId : newRoom.TagId;
            room.Name             = newRoom.Name ?? room.Name;
            room.Picture          = newRoom.Picture ?? room.Picture;
            room.Rule             = newRoom.Rule ?? room.Rule;
            room.TagText          = newRoom.TagText ?? room.TagText;
            room.MaxUsers         = newRoom.MaxUsers == 0 ? room.MaxUsers : newRoom.MaxUsers;
            room.Star             = newRoom.Star == 0 ? room.Star : newRoom.Star;
            room.R18              = newRoom.R18;
            room.RoomClose        = newRoom.RoomClose;
            room.RoomEnd          = DateTime.Now.AddHours(1);
            _db.Entry(room).State = EntityState.Modified;
            try
            {
                _db.SaveChanges();
                return(Ok(id));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
        public IHttpActionResult PostRoom([FromBody] Room room)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);

            if ((permission & 4) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            room.RoomStart = DateTime.Now;
            room.RoomEnd   = DateTime.Now.AddHours(1);
            if (room.RoomStart > DateTime.Now && room.RoomEnd <= DateTime.Now)
            {
                room.RoomClose = true;
            }
            room.SellerId = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);
            if (room.CountryId == 0)
            {
                room.CountryId = 1;
            }
            if (room.CountyId == 0)
            {
                room.CountyId = 1;
            }
            if (room.CityId == 0)
            {
                room.CityId = 1;
            }
            if (room.TagId == 0)
            {
                room.TagId = 1;
            }
            _db.Rooms.Add(room);
            try
            {
                _db.SaveChanges();
                return(Ok(room.Id));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
        public IHttpActionResult DeleteRoom(int id)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);

            if ((permission & 4) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            var room = _db.Rooms.Find(id);

            if (room == null)
            {
                return(NotFound());
            }
            var tokenId = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);

            if (tokenId != room.SellerId)
            {
                return(BadRequest("使用者錯誤"));
            }
            room.RoomClose        = true;
            _db.Entry(room).State = EntityState.Modified;
            try
            {
                _db.SaveChanges();
                return(Ok(id));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
            //var roomUser = room.RoomUsers.FirstOrDefault(user => user.RoomId == id);
            //if (roomUser != null) return BadRequest("房間還有訪客");
            //_db.Rooms.Remove(room);
            //try
            //{
            //    _db.SaveChanges();
            //    return Ok(id);
            //}
            //catch (Exception e)
            //{
            //    return BadRequest(e.Message);
            //}
        }
        public IHttpActionResult GetBuyerRatings()
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);

            if ((permission & 1) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            var tokenId = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);
            var orders  = _db.Orders.Where(order => order.BuyerId == tokenId).ToList();

            return(Ok(orders.OrderByDescending(order => order.Id).Select(order => new
            {
                order.Id,
                order.Name,
                order.Address,
                order.Email,
                order.Phone,
                Payment = order.Payment.ToString(),
                Pickup = order.Pickup.ToString(),
                Status = order.Status.ToString(),
                order.TotalPrice,
                order.Remark,
                order.RoomId,
                RoomName = order.Room.Name,
                RoomPicture = order.Room.Picture,
                Detail = order.OrderDetails.Select(detail => new
                {
                    detail.Id,
                    detail.Name,
                    detail.Price
                }).ToList(),
                order.SellerId,
                SellerNickname = order.Seller.Nickname,
                SellerAccount = order.Seller.Account,
                SellerPicture = order.Seller.Picture,
                SellerStar = Star(order.SellerStar),
                order.SellerReviews,
                BuyerStar = Star(order.BuyerStar),
                order.BuyerReviews,
            })));
        }
Exemple #7
0
        public IHttpActionResult PostOrder(InputId inputId)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);

            if ((permission & 2) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            var order = new Order
            {
                TotalPrice = 0,
                SellerId   = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter),
                BuyerId    = inputId.BuyerId,
                RoomId     = inputId.RoomId
            };
            var tempDetails = _db.TempDetails.Where(x => x.BuyerId == inputId.BuyerId && x.RoomId == inputId.RoomId);

            foreach (var detail in tempDetails)
            {
                var newDerail = new OrderDetail
                {
                    Name    = detail.Name,
                    Price   = detail.Price,
                    OrderId = order.Id
                };
                order.TotalPrice += detail.Price;
                _db.OrderDetails.Add(newDerail);
                //產生產品明細
            }
            //產生訂單表
            _db.Orders.Add(order);
            try
            {
                _db.SaveChanges();
                return(Ok(order.Id));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Exemple #8
0
        public IHttpActionResult PutUser(int id, [FromBody] User newUser)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);
            var tokenId    = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);

            if ((permission & 1) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            if (tokenId != id)
            {
                return(BadRequest("使用者錯誤"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var user = _db.Users.Find(id);

            user.PasswordSalt     = Salt.CreateSalt();
            user.Password         = Salt.GenerateHashWithSalt(newUser.Password, user.PasswordSalt);
            user.Nickname         = newUser.Nickname ?? user.Nickname;
            user.Name             = newUser.Name ?? user.Name;
            user.Picture          = newUser.Picture ?? user.Picture;
            user.Email            = newUser.Email ?? user.Email;
            user.Phone            = newUser.Phone ?? user.Phone;
            user.Birthday         = user.Birthday;
            _db.Entry(user).State = EntityState.Modified;
            try
            {
                _db.SaveChanges();
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
            return(Ok(id));
        }
Exemple #9
0
        public IHttpActionResult DeleteRoomUser(int id)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);

            if ((permission & 2) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            var tokenId  = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);
            var roomUser = _db.RoomUsers.FirstOrDefault(x => x.RoomId == id && x.UserId == tokenId);
            var delUser  = _db.RoomUsers.Find(roomUser.Id);

            _db.RoomUsers.Remove(delUser);
            try
            {
                _db.SaveChanges();
                return(Ok(id));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Exemple #10
0
        public IHttpActionResult PutRatings(int id, Ratings newRating)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);
            var tokenId    = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);
            var user       = _db.Users.Find(tokenId);

            if ((permission & 16) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var order = _db.Orders.Find(id);

            if (order == null)
            {
                return(NotFound());
            }
            if (order.SellerId == tokenId)
            {
                order.BuyerStar    = newRating.BuyerStar;
                order.BuyerReviews = newRating.BuyerReviews;
                if (order.Buyer.SellerAverageStar > 0)
                {
                    order.Buyer.SellerAverageStar += newRating.SellerStar;
                    order.Buyer.SellerAverageStar /= 2;
                }
                else
                {
                    order.Buyer.SellerAverageStar = newRating.SellerStar;
                }
            }
            else
            {
                order.SellerStar    = newRating.SellerStar;
                order.SellerReviews = newRating.SellerReviews;
                if (order.Seller.BuyerAverageStar > 0)
                {
                    order.Seller.BuyerAverageStar += newRating.BuyerStar;
                    order.Seller.BuyerAverageStar /= 2;
                }
                else
                {
                    order.Seller.BuyerAverageStar = newRating.BuyerStar;
                }
            }
            _db.Entry(user).State  = EntityState.Modified;
            _db.Entry(order).State = EntityState.Modified;
            try
            {
                _db.SaveChanges();
                return(Ok(new
                {
                    id,
                    order.SellerId,
                    order.BuyerId,
                    tokenId
                }));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Exemple #11
0
        public IHttpActionResult PostRoomUser([FromBody] Room room)
        {
            var permission = JwtAuth.GetTokenPermission(Request.Headers.Authorization.Parameter);

            if ((permission & 2) <= 0)
            {
                return(BadRequest("權限不足"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var roomData = _db.Rooms.Find(room.Id);

            if (roomData != null && roomData.RoomClose)
            {
                return(BadRequest("找不到房間"));
            }
            var roomUsers = _db.RoomUsers.Where(x => x.RoomId == room.Id);
            var tokenId   = JwtAuth.GetTokenId(Request.Headers.Authorization.Parameter);

            if (roomData.SellerId == tokenId)
            {
                // 房主直接進入
                return(Ok(roomUsers.Select(user => new
                {
                    user.RoomId,
                    user.Room.Name,
                    UserId = user.User.Id,
                    UserName = user.User.Name,
                    UserPicture = user.User.Picture,
                    user.Status
                })));
            }
            var joinedUser = roomUsers.Where(user => user.User.Id == tokenId);

            if (joinedUser.Any())
            {
                // 房客直接進入
                return(Ok(joinedUser.Select(user => new
                {
                    user.RoomId,
                    user.Room.Name,
                    UserId = user.User.Id,
                    UserName = user.User.Name,
                    UserPicture = user.User.Picture,
                    user.Status
                })));
            }
            if (roomData.MaxUsers < roomUsers.Count())
            {
                return(BadRequest("人數已經滿"));
            }
            // 不在房間內則進入
            var newUser = new RoomUser
            {
                RoomId = room.Id,
                UserId = tokenId,
                Status = UserStatus.無訂單
            };

            _db.RoomUsers.Add(newUser);
            try
            {
                _db.SaveChanges();
                return(Ok(roomUsers.Select(user => new
                {
                    user.RoomId,
                    user.Room.Name,
                    UserId = user.User.Id,
                    UserName = user.User.Name,
                    UserPicture = user.User.Picture,
                    user.Status
                })));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }