Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.Rendering;
- using Microsoft.EntityFrameworkCore;
- using GraniteHouse.Data;
- using NotDonkeyApp_UG.Models;
- using NotDonkeyApp_UG.Services;
- using NotDonkeyApp_UG.Data;
- namespace NotDonkeyApp_UG.Controllers
- {
- public class AnimalNotDonkeysController : Controller
- {
- private readonly ApplicationDbContext _db;
- public AnimalNotDonkeysController(ApplicationDbContext db)
- {
- _db = db;
- }
- public IActionResult Index()
- {
- var listOfAllUsers = _db.NotDonkeys.ToList();
- return View(listOfAllUsers);
- }
- public IActionResult AllAnimals()
- {
- if (!StaticDetails.DonkeysAvailableToLike.Any())
- {
- var listOfAllUsers = _db.NotDonkeys.ToList();
- StaticDetails.DonkeysAvailableToLike = listOfAllUsers;
- return View(listOfAllUsers);
- }
- else
- {
- return View(StaticDetails.DonkeysAvailableToLike);
- }
- }
- public IActionResult AddAnimalToFavourites(int id)
- {
- var user = _db.NotDonkeys.Find(StaticDetails.CurrentUserId);
- if (user != null)
- {
- user.AnimalsYouLike += $"{id.ToString()}.";
- StaticDetails.DonkeysAvailableToLike.Remove(StaticDetails.DonkeysAvailableToLike[id]);
- _db.SaveChanges();
- }
- return RedirectToAction("AllAnimals");
- }
- public IActionResult PairedAnimals()
- {
- var user = _db.NotDonkeys.Find(StaticDetails.CurrentUserId);
- if (user != null)
- {
- var allAnimalLikedids = AnimalService.Instance.ProceedUserIds(user.AnimalsYouLike ?? String.Empty);
- List<AnimalNotDonkey> yourAnimalsList = new List<AnimalNotDonkey>();
- foreach (var id in allAnimalLikedids)
- {
- yourAnimalsList.Add(_db.NotDonkeys.Find(id));
- }
- return View(yourAnimalsList);
- }
- return View(new List<AnimalNotDonkey>());
- }
- public IActionResult Login()
- {
- return View();
- }
- [HttpPost]
- public IActionResult Login(int ownerPhoneNumber, string password)
- {
- var user = _db.NotDonkeys.FirstOrDefault(x => x.OwnerPhoneNumber == ownerPhoneNumber);
- if (user.Password == password)
- {
- StaticDetails.CurrentUserId = user.Id;
- return RedirectToAction("PairedAnimals");
- }
- return View();
- }
- public IActionResult Create()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<IActionResult> Create([Bind("Id,OwnerPhoneNumber,Name,Password,AnimalType,Sex,Weight,AdditionalDescription,IsDonkey,AnimalsYouLike")] AnimalNotDonkey animalNotDonkey)
- {
- TryCatch(() => {
- if (ModelState.IsValid && !animalNotDonkey.IsDonkey)
- {
- animalNotDonkey.AnimalType = AnimalService.Instance.SetAnimalType(animalNotDonkey.AnimalType);
- _db.Add(animalNotDonkey);
- await _db.SaveChangesAsync();
- return View("Login");
- }
- return View(animalNotDonkey);
- }, "wyjebal sie Create");
- }
- public IActionResult ThrowErrorMessage()
- {
- return View(StaticDetails.CurrentErrorMessage);
- }
- #region Helper Methods
- private void TryCatch(Action metoda, string errorToDisplay)
- {
- try
- {
- StaticDetails.CurrentErrorMessage = String.Empty;
- metoda.Invoke();
- }
- catch (Exception e)
- {
- StaticDetails.CurrentErrorMessage = errorToDisplay + " reason : " + e.Message;
- return RedirectToAction("ThrowErrorMessage");
- }
- }
- #endregion
- }
- }
- @model IEnumerable<AnimalNotDonkey>
- @{
- ViewData["Title"] = "AllAnimals";
- }
- <h1>List of our current animals : </h1>
- <br />
- <table class="table table-bordered" width="100%">
- <tr>
- <th>Name</th>
- <th>Breed</th>
- <th>avatar</th>
- <th></th>
- </tr>
- @foreach (var animal in Model)
- {
- <tr>
- <td>@animal.Name</td>
- <td>@animal.AnimalType</td>
- <td>tu ma byc zdj</td>
- @if (animal.Id != StaticDetails.CurrentUserId)
- {
- <td><a asp-controller="AnimalNotDonkeys" asp-action="AddAnimalToFavourites" asp-route-Id="@animal.Id">Like It</a></td>
- <td><a asp-controller="AnimalNotDonkeys" asp-action="AddAnimalToFavourites" asp-route-Id="@animal.Id">Like It</a></td>
- }
- </tr>
- }
- </table>
- using NotDonkeyApp_UG.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace NotDonkeyApp_UG.Data
- {
- public static class StaticDetails
- {
- /// <summary>
- /// Currently logged user info
- /// </summary>
- public static int CurrentUserId { get; set; }
- /// <summary>
- /// AvatarUrl + AnimalType from AnimalNotDonkeyModel == path to img
- /// </summary>
- public static readonly string AvatarUrl = "";
- /// <summary>
- /// List will be edited after you like some animal
- /// </summary>
- public static List<AnimalNotDonkey> DonkeysAvailableToLike = new List<AnimalNotDonkey>();
- /// <summary>
- /// Will be assigned in catch block and displayed in ThrowErrorMessage View
- /// </summary>
- public static string CurrentErrorMessage { get; set; }
- }
- }
- TO JEST DO VIEW ThrowErrorMessage
- @model string
- @{
- ViewData["Title"] = "Login";
- }
- <h1>An Error occured</h1>
- <br />
- <h2>@model</h2>
- <br /><br />
- <img/> // tutaj ma byc zdjecie osiolka
- <br /><br />
- <a asp-controller="AnimalNotDonkeys" asp-action="Index" >Back To Main Page</a>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement