Ejemplo n.º 1
0
        public IActionResult DeleteFromAll(ReadingDeleteFromAllViewModel model)
        {
            if (model.Id == null)
            {
                return(RedirectToAction("AllFromUser", "Reading", new { rmessage = ReadingMessageId.ReadingNotFound }));
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction("AllFromUser", "Reading", new { rmessage = ReadingMessageId.ValidationFailed }));
            }

            try
            {
                Reading reading = _readingsRepo.GetReading(model.Id);

                string authUserId = GetCurrentUserAsync().Result.Id;
                if (!reading.Owner.UserId.Equals(authUserId))
                {
                    return(RedirectToAction("AllFromUser", "Reading", new { rmessage = ReadingMessageId.NotAuthorized }));
                }

                _readingsRepo.Delete(reading);
                _logger.LogInformation("User deleted one Reading.");
                return(RedirectToAction("AllFromUser", "Reading", new { rmessage = ReadingMessageId.DeleteReadingSuccess }));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(RedirectToAction("AllFromUser", "Reading", new { rmessage = ReadingMessageId.DeleteReadingFailed }));
            }
        }
Ejemplo n.º 2
0
        public IActionResult DeleteFromAll(string readingId)
        {
            if (readingId == null)
            {
                return(RedirectToAction("AllFromUser", "Reading", new { rmessage = ReadingMessageId.ReadingNotFound }));
            }

            Reading reading = _readingsRepo.GetReading(readingId);

            if (reading == null)
            {
                return(RedirectToAction("AllFromUser", "Reading", new { rmessage = ReadingMessageId.ReadingNotFound }));
            }

            if (!reading.Owner.UserId.Equals(GetCurrentUserAsync().Result.Id))
            {
                return(RedirectToAction("AllFromUser", "Reading", new { rmessage = ReadingMessageId.NotAuthorized }));
            }

            var model = new ReadingDeleteFromAllViewModel
            {
                Id        = reading.Id,
                Name      = reading.Name,
                ReadValue = reading.ReadValue,
                Timestamp = reading.Timestamp
            };

            ViewData["readingType"] = reading.ReadingType.Name;

            return(PartialView(model));
        }