示例#1
0
 public void UnserializeData(BinaryReader reader)
 {
     sourceHash      = reader.ReadHash();
     destinationHash = reader.ReadHash();
     settleHash      = reader.ReadHash();
     time            = new Timestamp(reader.ReadUInt32());
     this.status     = (SwapStatus)reader.ReadByte();
 }
示例#2
0
        public async Task <IActionResult> UpdateStatus([FromRoute] long id, SwapStatus swapStatus)
        {
            var swap = _dbContext.Swaps.Include(m => m.User).Include(m => m.Sticker).ThenInclude(m => m.User).FirstOrDefault(m => m.Id == id);

            if (swap == null)
            {
                return(NotFound());
            }

            var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;

            // Can only transition to correct state
            switch (swap.Status)
            {
            case SwapStatus.Processing:
                if (swapStatus != SwapStatus.Cancelled && swapStatus != SwapStatus.Shipped)
                {
                    return(BadRequest());
                }
                break;

            case SwapStatus.Shipped:
                if (swapStatus != SwapStatus.Complete)
                {
                    return(BadRequest());
                }
                break;
            }

            // Only users that are part of this order can cancel it
            if (swap.Status == SwapStatus.Processing && swapStatus == SwapStatus.Cancelled)
            {
                if (swap.User.Id != userId && swap.Sticker.User.Id != userId)
                {
                    return(BadRequest());
                }
            }

            // Only the product author can ship the item
            if (swap.Status == SwapStatus.Processing && swapStatus == SwapStatus.Shipped)
            {
                if (swap.Sticker.User.Id != userId)
                {
                    return(BadRequest());
                }
            }

            // Only the order owner can complete the request
            if (swap.Status == SwapStatus.Shipped && swapStatus != SwapStatus.Complete)
            {
                if (swap.User.Id != userId)
                {
                    return(BadRequest());
                }
            }

            if (swapStatus == SwapStatus.Cancelled)
            {
                var targetUser = userId == swap.User.Id ? swap.User : swap.Sticker.User;
                var sourceUser = userId != swap.User.Id ? swap.User : swap.Sticker.User;

                if (targetUser.EnableEmail)
                {
                    await _emailSender.SendEmailAsync(targetUser.Email, "Your Sticker Swap has been cancelled!", $"Your Sticker Swap been cancelled by {sourceUser.Email}. The quantity of stickers has been reset.");
                }


                swap.User.Credits     += swap.Credits;
                swap.Sticker.Quantity += swap.Quantity;
            }

            if (swapStatus == SwapStatus.Shipped)
            {
                if (swap.User.EnableEmail)
                {
                    await _emailSender.SendEmailAsync(swap.User.Email, "You sticker has been shipped!", $"Your sticker is on the way! Make sure to mark the swap complete when you get the sticker.");
                }
            }

            if (swapStatus == SwapStatus.Complete)
            {
                if (swap.Sticker.User.EnableEmail)
                {
                    await _emailSender.SendEmailAsync(swap.Sticker.User.Email, "Your Sticker Swap has completed!", $"Your Sticker Swap has completed!");
                }
            }

            swap.Status = swapStatus;
            await _dbContext.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }