Beispiel #1
0
        public IActionResult Change([FromForm] IFormCollection form, [FromForm] IFormFile picFile)        //string name, string surname, string email,  string telephone, string password)
        {
            //int creatorId = int.Parse(Request.Cookies["user"]);
            int      id              = int.Parse(form["id"]);
            var      title           = form["title"];
            DateTime dateStart       = DateTime.Parse(form["dateStart"]);
            DateTime dateEnd         = DateTime.Parse(form["dateEnd"]);
            var      city            = form["city"];
            var      street          = form["street"];
            var      houseNumber     = int.Parse(form["houseNumber"]);
            var      apartmentNumber = int.Parse(form["apartmentNumber"]);
            var      postalCode      = form["postalCode"];
            var      description     = form["description"];

            byte[] picture = null;


            if (picFile is null)
            {
                Event culturalEvent = new Event
                {
                    Id              = id,
                    Title           = title,
                    DateStart       = dateStart,
                    DateEnd         = dateEnd,
                    City            = city,
                    Street          = street,
                    HouseNumber     = houseNumber,
                    ApartmentNumber = apartmentNumber,
                    PostalCode      = postalCode,
                    Description     = description,
                    Picture         = _events.GetPicture(id),
                    CreatorId       = _events.GetCreatorId(id)
                };

                try
                {
                    //save
                    _events.Edit(culturalEvent);
                    return(Redirect("/Events/Created"));
                }
                catch (Exception ex)
                {
                    //return error if there was an exception
                    return(BadRequest(new { message = ex.Message }));
                }
            }
            else
            {
                BinaryReader reader = new BinaryReader(picFile.OpenReadStream());
                picture = reader.ReadBytes((int)picFile.Length);

                Event culturalEvent = new Event
                {
                    Id              = id,
                    Title           = title,
                    DateStart       = dateStart,
                    DateEnd         = dateEnd,
                    City            = city,
                    Street          = street,
                    HouseNumber     = houseNumber,
                    ApartmentNumber = apartmentNumber,
                    PostalCode      = postalCode,
                    Description     = description,
                    Picture         = picture,
                    CreatorId       = _events.GetCreatorId(id)
                };

                try
                {
                    //save
                    _events.Edit(culturalEvent);
                    return(Redirect("Events/Created"));
                }
                catch (Exception ex)
                {
                    //return error if there was an exception
                    return(BadRequest(new { message = ex.Message }));
                }
            }
        }