Example #1
0
        public async Task <IActionResult> New(GenricApartment <ListsVM, RentVM, SaleVM, ApartmentVM> apartment, IFormFile file)
        {
            Messages msg = new Messages();

            if (!ModelState.IsValid)
            {
                msg.SetMessage(ToastTypes.error, "There is an error in the entered data.", "Error");
            }
            else
            {
                try
                {
                    IPayload entity = (apartment.In2, apartment.In3) switch
                    {
                        (null, _) => apartment.In3.Sale,
                        (_, null) => apartment.In2.Rent,
                        _ => throw new ArgumentOutOfRangeException("Invalid arguments values.")
                    };
                    Guid requestId = Guid.NewGuid();
                    var  payload   = new Payload <IPayload>(apartment.In4.Apartment, entity);
                    payload.Apartment.RequestId = requestId;
                    await _apartmentService.Save(payload);

                    ModelState.Clear();
                    await _apartmentService.UploadImage(file, requestId);

                    msg.SetMessage(ToastTypes.success, "Apartment data was saved.", "Success");
                }
                catch (Exception ex)
                {
                    if (ex.GetType() == typeof(ArgumentException))
                    {
                        msg.SetMessage(ToastTypes.warning, ex.Message ?? "Apartment service is inoperative.", "Warning");
                    }
                    else
                    {
                        msg.SetMessage(ToastTypes.error, ex.Message ?? "Apartment service is inoperative.");
                    }
                }
            }
            try
            {
                ViewBag.msg = msg;
                string result = await _apartmentService.PopulateLists();

                var model = PopulateLists(result, (Rent rent, Sale sale, Apartment apartData) =>
                {
                    rent      = apartment.In2?.Rent;
                    sale      = apartment.In3?.Sale;
                    apartData = apartment.In4?.Apartment;
                });
                return(ActionResult(model));
            }
            catch
            {
                return(RedirectToAction(nameof(Blank)));
            }
        }
Example #2
0
 public ActionResult Post([FromBody] Apartment apartment)
 {
     return(Ok(apartmentService.Save(apartment)));
 }