Beispiel #1
0
        public override void Post(HttpRequest Request, HttpResponse Response, params string[] PathParams)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetMaxAge(TimeSpan.Zero);

            JObject inputData = null;

            try
            {
                using (StreamReader reader = new StreamReader(Request.InputStream))
                {
                    using (JsonTextReader jsonReader = new JsonTextReader(reader))
                    {
                        inputData = JObject.Load(jsonReader);
                    }
                }
            }
            catch
            {
                RespondBadRequest(Response);
            }


            Response.ContentType = @"application/json";
            Int64 SupplierId;

            if (IsAuthorizedRequestSupplier(Request, Response, true, out SupplierId))
            {
                JToken  jt;
                Int64   BidId = 0;
                decimal total_price = 0;
                string  gift = null, supplier_remarks = null;
                bool    is_service = false;
                if (inputData.TryGetValue(@"bid_id", out jt))
                {
                    BidId = jt.Value <Int64>();
                }
                if (inputData.TryGetValue(@"total_price", out jt))
                {
                    total_price = jt.Value <decimal>();
                }
                if (inputData.TryGetValue(@"gift", out jt))
                {
                    gift = jt.Value <string>();
                }
                if (inputData.TryGetValue(@"supplier_remarks", out jt))
                {
                    supplier_remarks = Regex.Replace(jt.Value <string>(), @"\p{Cs}", "");
                }
                ;
                if (inputData.TryGetValue(@"is_service", out jt))
                {
                    is_service = jt.Value <bool>();
                }

                using (StreamWriter streamWriter = new StreamWriter(Response.OutputStream))
                {
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                    {
                        if (is_service)
                        {
                            OfferService offer = OfferService.FetchByBidIdAndSupplierId(BidId, SupplierId);
                            if (offer != null)
                            {
                                RespondError(Response, HttpStatusCode.Found, "offer-already-exists");
                            }
                            BidService b = BidService.FetchByID(BidId);
                            if (b != null && b.EndDate <= DateTime.UtcNow)
                            {
                                RespondError(Response, HttpStatusCode.BadRequest, "bid-expiry-date");
                            }
                            offer                 = new OfferService();
                            offer.BidId           = BidId;
                            offer.CreateDate      = DateTime.UtcNow;
                            offer.SupplierRemarks = supplier_remarks;
                            offer.Price           = total_price;
                            offer.SupplierId      = SupplierId;
                            offer.Save();

                            jsonWriter.WriteStartObject();
                            jsonWriter.WritePropertyName(@"offer_id");
                            jsonWriter.WriteValue(offer.OfferId);
                            jsonWriter.WriteEndObject();
                        }
                        else if (total_price > 0)
                        {
                            Offer offer = Offer.FetchByBidIdAndSupplierId(BidId, SupplierId);
                            if (offer != null)
                            {
                                RespondError(Response, HttpStatusCode.Found, "offer-already-exists");
                            }
                            Bid b = Bid.FetchByID(BidId);
                            if (b != null && b.EndDate <= DateTime.UtcNow)
                            {
                                RespondError(Response, HttpStatusCode.BadRequest, "bid-expiry-date");
                            }
                            offer            = new Offer();
                            offer.BidId      = BidId;
                            offer.CreateDate = DateTime.UtcNow;
                            offer.Gift       = gift;
                            offer.Price      = total_price;
                            offer.SupplierId = SupplierId;
                            offer.Save();

                            jsonWriter.WriteStartObject();
                            jsonWriter.WritePropertyName(@"offer_id");
                            jsonWriter.WriteValue(offer.OfferId);
                            jsonWriter.WriteEndObject();
                        }
                        else
                        {
                            RespondError(Response, HttpStatusCode.BadRequest, @"price-zero");
                        }
                    }
                }
            }
        }