<commit_before>﻿using System;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ReservationSystem.Common;
using ReservationSystem.Extensions;
using ReservationSystem.Services.Data.Contracts;
using ReservationSystem.ViewComponents;
using ReservationSystem.ViewModels.CalendarViewModels;

namespace ReservationSystem.Controllers
{
  public class CalendarController : Controller
  {
    private readonly IMapper mapper;
    private readonly IUnitOfWork unitOfWork;
    private readonly IReservationService reservationService;

    public CalendarController(IMapper mapper, IUnitOfWork unitOfWork, IReservationService reservationService)
    {
      this.mapper = mapper;
      this.unitOfWork = unitOfWork;
      this.reservationService = reservationService;
    }

    [HttpGet]
    public IActionResult Index()
    {
      var currentDate = DateTime.UtcNow.Date;

      return this.View(currentDate);
    }

    [HttpPost]
    public IActionResult GetPreviousMonth(long selectedYearMonth)
    {
      var newSelectedYearMonth = new DateTime(selectedYearMonth).ToUniversalTime().Date;
      var selectedYearMonthPrevious = newSelectedYearMonth.Date.AddMonths(-1);

      return this.ViewComponent(typeof(CalendarViewComponent), new { selectedYearMonth = selectedYearMonthPrevious });
    }

    [HttpPost]
    public IActionResult GetNextMonth(long selectedYearMonth)
    {
      var newSelectedYearMonth = new DateTime(selectedYearMonth).ToUniversalTime().Date;
      var selectedYearMonthNext = newSelectedYearMonth.Date.AddMonths(1);

      return this.ViewComponent(typeof(CalendarViewComponent), new { selectedYearMonth = selectedYearMonthNext });
    }

    [HttpPost]
    [Authorize]
    [ValidateAntiForgeryToken]
    public IActionResult ReservateTheDate(DateTime date)
    {
      if (!this.User.Identity.IsAuthenticated || this.User.IsInRole(GlobalConstants.AdministratorRoleName))
      {
        return this.View("Index", date);
      }

      if (date.Date < DateTime.UtcNow.Date)
      {
        return this.View("Index", date);
      }

      var reservation = this.reservationService.GetByDate(date);

      if (!string.IsNullOrWhiteSpace(reservation.OwnerId))
      {
        return this.View("Index", date);
      }

      var userId = this.User.GetUserId();
      reservation.OwnerId = userId;

      this.reservationService.Update(reservation);
      this.unitOfWork.SaveChanges();<commit_msg>refactor ReservateTheDate()<commit_after>﻿using System;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ReservationSystem.Common;
using ReservationSystem.Extensions;
using ReservationSystem.Services.Data.Contracts;
using ReservationSystem.ViewComponents;
using ReservationSystem.ViewModels.CalendarViewModels;

namespace ReservationSystem.Controllers
{
  public class CalendarController : Controller
  {
    private readonly IMapper mapper;
    private readonly IUnitOfWork unitOfWork;
    private readonly IReservationService reservationService;

    public CalendarController(IMapper mapper, IUnitOfWork unitOfWork, IReservationService reservationService)
    {
      this.mapper = mapper;
      this.unitOfWork = unitOfWork;
      this.reservationService = reservationService;
    }

    [HttpGet]
    public IActionResult Index()
    {
      var currentDate = DateTime.UtcNow.Date;

      return this.View(currentDate);
    }

    [HttpPost]
    public IActionResult GetPreviousMonth(long selectedYearMonth)
    {
      var newSelectedYearMonth = new DateTime(selectedYearMonth).ToUniversalTime().Date;
      var selectedYearMonthPrevious = newSelectedYearMonth.Date.AddMonths(-1);

      return this.ViewComponent(typeof(CalendarViewComponent), new { selectedYearMonth = selectedYearMonthPrevious });
    }

    [HttpPost]
    public IActionResult GetNextMonth(long selectedYearMonth)
    {
      var newSelectedYearMonth = new DateTime(selectedYearMonth).ToUniversalTime().Date;
      var selectedYearMonthNext = newSelectedYearMonth.Date.AddMonths(1);

      return this.ViewComponent(typeof(CalendarViewComponent), new { selectedYearMonth = selectedYearMonthNext });
    }

    [HttpPost]
    [Authorize]
    [ValidateAntiForgeryToken]
    public IActionResult ReservateTheDate(DateTime date)
    {
      if (!this.User.Identity.IsAuthenticated || this.User.IsInRole(GlobalConstants.AdministratorRoleName))
      {
        return this.View("Index", date);
      }

      if (date.ToUniversalTime().Date < DateTime.UtcNow.Date)
      {
        return this.View("Index", date);
      }

      var reservation = this.reservationService.GetByDate(date);

      if (!string.IsNullOrWhiteSpace(reservation.OwnerId))
      {
        return this.View("Index", date);
      }

      var userId = this.User.GetUserId();
      reservation.OwnerId = userId;

      this.reservationService.Update(reservation);
      this.unitOfWork.SaveChanges();
{'fraction_non_alphanumeric': 0.08542003231017771, 'fraction_numerical': 0.0008077544426494346, 'mean_word_length': 3.948051948051948, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3437065', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1339, 'n_words': 252}