Esempio n. 1
0
        public async Task <IActionResult> PostLuminary([FromBody] LuminaryRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Foto1
            var imageUrl1 = string.Empty;

            if (request.BasePhotoArray != null && request.BasePhotoArray.Length > 0)
            {
                var stream   = new MemoryStream(request.BasePhotoArray);
                var guid     = Guid.NewGuid().ToString();
                var file     = $"{guid}.jpg";
                var folder   = "wwwroot\\images\\Luminaries";
                var fullPath = $"~/images/Luminaries/{file}";
                var response = FilesHelper.UploadPhoto(stream, folder, file);

                if (response)
                {
                    imageUrl1 = fullPath;
                }
            }
            //Foto2
            var imageUrl2 = string.Empty;

            if (request.TopPhotoArray != null && request.TopPhotoArray.Length > 0)
            {
                var stream   = new MemoryStream(request.TopPhotoArray);
                var guid     = Guid.NewGuid().ToString();
                var file     = $"{guid}.jpg";
                var folder   = "wwwroot\\images\\Luminaries";
                var fullPath = $"~/images/Luminaries/{file}";
                var response = FilesHelper.UploadPhoto(stream, folder, file);

                if (response)
                {
                    imageUrl2 = fullPath;
                }
            }
            //Foto3
            var imageUrl3 = string.Empty;

            if (request.FullPhotoArray != null && request.FullPhotoArray.Length > 0)
            {
                var stream   = new MemoryStream(request.FullPhotoArray);
                var guid     = Guid.NewGuid().ToString();
                var file     = $"{guid}.jpg";
                var folder   = "wwwroot\\images\\Luminaries";
                var fullPath = $"~/images/Luminaries/{file}";
                var response = FilesHelper.UploadPhoto(stream, folder, file);

                if (response)
                {
                    imageUrl3 = fullPath;
                }
            }

            User User2 = await _context.Users
                         .Include(n => n.Neighborhood)
                         .FirstOrDefaultAsync(o => o.Id == request.UserId);


            var luminary = new Luminary
            {
                Date         = request.Date,
                Id           = request.Id,
                Address      = request.Address,
                BasePhoto    = imageUrl1,
                TopPhoto     = imageUrl2,
                FullPhoto    = imageUrl3,
                Type         = request.Tipo,
                Remarks      = request.Remarks,
                User         = User2,
                Latitude     = request.Latitude,
                Longitude    = request.Longitude,
                Neighborhood = User2.Neighborhood,
                State        = request.State,
            };

            _context.Luminaries.Add(luminary);
            await _context.SaveChangesAsync();

            return(Ok(_converterHelper.ToLuminaryResponse(luminary)));
            //return NoContent();
        }
Esempio n. 2
0
        public async Task <IActionResult> PutLuminary([FromRoute] int id, [FromBody] LuminaryRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != request.Id)
            {
                return(BadRequest());
            }

            var oldLuminary = await _context.Luminaries.FindAsync(request.Id);

            if (oldLuminary == null)
            {
                return(BadRequest("La Luminaria no existe."));
            }

            var imageUrl1 = oldLuminary.BasePhoto;

            if (request.BasePhotoArray != null && request.BasePhotoArray.Length > 0)
            {
                var stream   = new MemoryStream(request.BasePhotoArray);
                var guid     = Guid.NewGuid().ToString();
                var file     = $"{guid}.jpg";
                var folder   = "wwwroot\\images\\Luminaries";
                var fullPath = $"~/images/Luminaries/{file}";
                var response = FilesHelper.UploadPhoto(stream, folder, file);

                if (response)
                {
                    imageUrl1 = fullPath;
                }
            }

            var imageUrl2 = oldLuminary.TopPhoto;

            if (request.TopPhotoArray != null && request.TopPhotoArray.Length > 0)
            {
                var stream   = new MemoryStream(request.TopPhotoArray);
                var guid     = Guid.NewGuid().ToString();
                var file     = $"{guid}.jpg";
                var folder   = "wwwroot\\images\\Luminaries";
                var fullPath = $"~/images/Luminaries/{file}";
                var response = FilesHelper.UploadPhoto(stream, folder, file);

                if (response)
                {
                    imageUrl2 = fullPath;
                }
            }

            var imageUrl3 = oldLuminary.FullPhoto;

            if (request.FullPhotoArray != null && request.FullPhotoArray.Length > 0)
            {
                var stream   = new MemoryStream(request.FullPhotoArray);
                var guid     = Guid.NewGuid().ToString();
                var file     = $"{guid}.jpg";
                var folder   = "wwwroot\\images\\Luminaries";
                var fullPath = $"~/images/Luminaries/{file}";
                var response = FilesHelper.UploadPhoto(stream, folder, file);

                if (response)
                {
                    imageUrl3 = fullPath;
                }
            }

            oldLuminary.Address   = request.Address;
            oldLuminary.BasePhoto = imageUrl1;
            oldLuminary.TopPhoto  = imageUrl2;
            oldLuminary.FullPhoto = imageUrl3;
            oldLuminary.Date      = request.Date;
            oldLuminary.Id        = request.Id;
            oldLuminary.Latitude  = request.Latitude;
            oldLuminary.Longitude = request.Longitude;
            oldLuminary.Type      = request.Tipo;
            oldLuminary.State     = request.State;
            oldLuminary.Remarks   = request.Remarks;
            oldLuminary.User      = await _context.Users.FindAsync(request.UserId);


            _context.Luminaries.Update(oldLuminary);
            await _context.SaveChangesAsync();

            return(Ok());
        }