コード例 #1
0
        public ResponseItem Put(string offerId, [FromForm] string action, [FromForm] string message)
        {
            //Schimbarile care se pot face sunt:
            //- sa accepte oferta initiala
            //- creatorul sa dea acordul SAU refuzul la acceptarea anterioara
            //- o parte inchide

            User user = HttpContext.Items["user"] as User;

            if (_offerService.Exist(offerId) == false)
            {
                return(new ResponseItem("error", "Oferta nu exista."));
            }

            Offer offer = _offerService.GetIfAccess(offerId, user.Id);

            if (offer == null)
            {
                return(new ResponseItem("error", "Nu ai acces la aceasta oferta."));
            }

            User userFrom = _userService.Get(offer.FromUser);

            if (action == "accept")
            {
                if (offer.Status != "ready")
                {
                    return(new ResponseItem("error", "Ceva nu mers bine. (Oferta este deja in rulare)"));
                }

                if (offer.FromUser == user.Id)
                {
                    return(new ResponseItem("error", "Ceva nu mers bine. (Nu poti accepta propriile oferte)"));
                }

                string updateError = _offerService.Accept(offer.Id, user.Id);
                return(Program.GetResponseItemFromError(updateError, "Oferta a fost acceptata."));
            }

            if (action == "aprove")
            {
                if (offer.Status != "waiting")
                {
                    return(new ResponseItem("error", "Ceva nu mers bine. (Oferta nu se poate aproba)"));
                }

                string updateError = _offerService.Aprove(offer.Id);
                return(Program.GetResponseItemFromError(updateError, "Oferta a fost inceputa."));
            }
            if (action == "reject")
            {
                if (offer.Status != "waiting")
                {
                    return(new ResponseItem("error", "Ceva nu mers bine. (Oferta nu se poate refuza)"));
                }

                string updateError = _offerService.Reject(offer.Id, message);
                return(Program.GetResponseItemFromError(updateError, "Oferta a fost refuzata."));
            }
            if (action == "close")
            {
                if (offer.Status != "running")
                {
                    return(new ResponseItem("error", "Ceva nu mers bine. (Oferta nu este in rulare, deci nu o poti inchide.)"));
                }
                if (offer.FromUser != user.Id)
                {
                    return(new ResponseItem("error", "Ceva nu mers bine. (Tu nu poti inchide aceasta oferta.)"));
                }

                string updateError = _offerService.Close(offer.Id);
                return(Program.GetResponseItemFromError(updateError, "Oferta a fost inchisa."));
            }
            return(new ResponseItem("success", "Success."));
        }