예제 #1
0
        public IActionResult OnPostAsync()
        {
            Input.UserName = Input.Email;

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var avatarUpload = Input.AvatarUpload;


            var    fullFilePath  = "";
            string locationToUse = "";

            if (avatarUpload != null)
            {
                //get previous image from folder
                string[] filePaths = Directory.GetFiles(@"wwwroot/images", "*", SearchOption.TopDirectoryOnly);

                var oldFile = filePaths.FirstOrDefault(p => p.Contains(this.User.Identity.Name));
                if (System.IO.File.Exists(oldFile))
                {
                    System.IO.File.Delete(oldFile);
                }



                locationToUse = "/images/" + Input.Email + "-" + avatarUpload.FileName;

                fullFilePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", Input.Email + "-" + avatarUpload.FileName);

                var fileStream = new FileStream(fullFilePath, FileMode.Create);

                avatarUpload.CopyTo(fileStream);

                fileStream.Close();
            }

            var user = _context.Users.Find(_userManager.GetUserId(User));

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }


            if (avatarUpload != null)
            {
                user.AvatarUrl = locationToUse;
            }

            user.PhoneNumber = Input.PhoneNumber;
            user.DateOfBirth = Input.DateOfBirth;
            user.FullName    = Input.FullName;

            _context.Users.Update(user);

            _context.SaveChanges();


            bool isLoggedIn = false;
            bool isAdmin    = false;

            var currentUser = this.User;

            if (currentUser.Identity.IsAuthenticated)
            {
                isLoggedIn = true;
                isAdmin    = currentUser.Claims.Any(c => c.Value == "Admin");
            }

            ViewData["LoggedIn"] = isLoggedIn.ToString();
            ViewData["IsAdmin"]  = isAdmin.ToString();


            StatusMessage = "Your profile has been updated";
            return(RedirectToPage());
        }
예제 #2
0
        public void PetConfirmFounder(int messageId)
        {
            Message message = this.context.Messages.Include(m => m.Likes).FirstOrDefault(m => m.Id == messageId);

            User currentUser = this.context.Users.FirstOrDefault(u => u.Email == this.User.Identity.Name);

            User claimerUser = this.context.Users.FirstOrDefault(u => u.Id == message.SenderId);

            var messageContent = message
                                 .Content.Split(" ").ToList();

            messageContent.RemoveRange(0, 9);
            messageContent.Reverse();
            messageContent.RemoveRange(0, 4);
            messageContent.Reverse();

            string petName = String.Join(" ", messageContent.ToArray());

            Pet pet = context.Pets
                      .Include(p => p.Owner)
                      .FirstOrDefault(p => p.Name == petName && p.Owner.Email == currentUser.Email);

            if (pet != null)
            {
                if (claimerUser != null)
                {
                    if (pet.Status != StaticConstants.Found)
                    {
                        //send messages to both users
                        string contentForFounder = "Owner confirmation completed! You found pet " + pet.Name + " of type " + pet.Type + " on " + message.CreationDate + ".";

                        Message foundMessageForFounder = new Message()
                        {
                            CreationDate = DateTime.Now,
                            Content      = contentForFounder,
                            LikeDisabled = false,
                            Likes        = new List <Like>(),
                            ReceverId    = claimerUser.Id,
                            SenderId     = currentUser.Id
                        };

                        string content = "Confirmation completed! " + claimerUser.Email + " found your pet " + pet.Name + " on " + message.CreationDate + ".";

                        Message foundMessage = new Message()
                        {
                            CreationDate = DateTime.Now,
                            Content      = content,
                            LikeDisabled = false,
                            Likes        = new List <Like>(),
                            ReceverId    = currentUser.Id,
                            SenderId     = claimerUser.Id
                        };

                        this.context.Messages.Add(foundMessageForFounder);
                        this.context.Messages.Add(foundMessage);
                        this.context.SaveChanges();

                        pet.Status    = StaticConstants.Found;
                        pet.TimeFound = message.CreationDate;
                        pet.FounderId = claimerUser.Id;
                        context.Pets.Update(pet);
                        context.SaveChanges();

                        claimerUser.PetsFound.Add(pet);

                        switch (pet.Type)
                        {
                        case StaticConstants.Other:
                            claimerUser.FeedBack = claimerUser.FeedBack + fifty;
                            break;

                        case StaticConstants.Dog:
                            claimerUser.FeedBack = claimerUser.FeedBack + oneHundred;
                            break;

                        case StaticConstants.Cat:
                            claimerUser.FeedBack = claimerUser.FeedBack + twoHundred;
                            break;

                        case StaticConstants.Bird:
                            claimerUser.FeedBack = claimerUser.FeedBack + threeHundred;
                            break;

                        default:
                            break;
                        }

                        claimerUser.PetsFound.Add(pet);
                        context.Users.Update(claimerUser);
                        context.SaveChanges();


                        foreach (Like like in message.Likes)
                        {
                            this.context.Likes.Remove(like);
                        }

                        this.context.SaveChanges();

                        this.context.Messages.Remove(message);

                        this.context.SaveChanges();
                    }
                }
            }
        }