Example #1
0
        public async static Task <MaskDonationModel> UpdateStatus(DataContext dataContext, Guid id, DonationStatus donationStatus)
        {
            var maskDonation = await Get(dataContext, id);

            if (maskDonation == null)
            {
                throw new ErrorModelException(ErrorCode.NotFound, "Donation");
            }

            if (maskDonation.Status != donationStatus)
            {
                maskDonation.Status = donationStatus;
                if (maskDonation.Status == DonationStatus.Received)
                {
                    var maskRequest = await MaskRequest.Get(dataContext, maskDonation.RequestId);

                    maskRequest.MaskDetails.Masks = maskRequest.MaskDetails.Masks.Join(maskDonation.Donation, m => m.Type, d => d.Type, (m, d) => {
                        var quantity = m.Quantity - d.Quantity;
                        return(new MaskInfo
                        {
                            Type = m.Type,
                            Quantity = quantity >= 0 ? quantity : 0
                        });
                    }).ToList();

                    await AddDonatedMasksToAggregate(dataContext, maskDonation);
                }
            }

            return(maskDonation.ToMaskDonationModel());
        }
Example #2
0
        public async static Task <MaskDonationModel> Update(DataContext dataContext, Guid id, MaskDonationModel maskDonationModel)
        {
            var maskDonation = maskDonationModel.ToMaskDonation();

            maskDonation.PartitionKey = partitionKey;
            maskDonation.Approved     = false;
            maskDonation.OriginalId   = id;
            maskDonation.Id           = Guid.NewGuid();
            maskDonation.CreatedOn    = DateTime.UtcNow;
            maskDonation.Request      = await MaskRequest.Get(dataContext, Guid.Parse(maskDonationModel.RequestId));

            await dataContext.SaveChangesAsync();

            return(maskDonation.ToMaskDonationModel());
        }
Example #3
0
        public async static Task <MaskDonationModel> Create(DataContext dataContext, MaskDonationModel maskDonationModel)
        {
            maskDonationModel.PartitionKey = partitionKey;
            maskDonationModel.CreatedOn    = DateTime.UtcNow;

            var maskDonation = maskDonationModel.ToMaskDonation();

            maskDonation.Request = await MaskRequest.Get(dataContext, maskDonation.RequestId);

            maskDonation.EditKey = Guid.NewGuid();

            dataContext.MaskDonations.Add(maskDonation);
            await dataContext.SaveChangesAsync();

            return(maskDonation.ToMaskDonationModel());
        }
Example #4
0
        public async static Task <MaskDonationModel> GetModel(DataContext dataContext, Guid id)
        {
            var maskDonation = await Get(dataContext, id);

            if (maskDonation == null)
            {
                throw new ErrorModelException(ErrorCode.NotFound, "Donation");
            }

            if (maskDonation.Request == null)
            {
                maskDonation.Request = await MaskRequest.Get(dataContext, maskDonation.RequestId);
            }

            return(maskDonation.ToMaskDonationModel());
        }
Example #5
0
        public async static Task UpdateOriginalId(DataContext dataContext, Guid originalId, Guid newId)
        {
            var oldMaskRequest = await MaskRequest.GetApproved(dataContext, originalId, true);

            if (oldMaskRequest != null)
            {
                var oldRefMaskRequests = dataContext.MaskRequests.Where(l => l.PartitionKey == partitionKey &&
                                                                        l.OriginalId == oldMaskRequest.Id && l.Id != newId);

                await oldRefMaskRequests.ForEachAsync((maskRequest) =>
                {
                    maskRequest.OriginalId = newId;
                });

                dataContext.Remove(oldMaskRequest);
            }
        }