<commit_before>package com.dta.services.controller;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dta.services.model.Advert;
import com.dta.services.model.AdvertType;
import com.dta.services.model.Category;
import com.dta.services.model.User;
import com.dta.services.service.IAdvertService;
import com.dta.services.service.IUserService;

@Controller
@RequestMapping("/")
public class AdvertController {
	
	@Autowired
	IAdvertService advertService;
	
	@Autowired
	IUserService userService;
	
	@ModelAttribute(value="user")
	public User user(){
		return new User();
	}
	
	@ModelAttribute(value="advert")
	public Advert advert(){
		return new Advert();
	}
	
	@RequestMapping
	public String home(Model model){
		
		return "Home";
	}
	
	@RequestMapping(value = "adverts")
	public String listAdverts(Model model) {

		return "Advertsview";
	}

	@Secured("hasAnyRole('USER', 'ADMIN')")
	@RequestMapping(value = "advert/new/{id}", method = RequestMethod.GET)
	public String postAdvert(@PathVariable long id, Model model) {

		List<Category> categories = advertService.getAllCategory();
		model.addAttribute("categories", categories);

		User user = userService.get(id);
		
		model.addAttribute("myUser", user);
		
		model.addAttribute("advert", new Advert());

		return "PostAdvert";
	}

	@Secured("hasAnyRole('USER', 'ADMIN')")
	@RequestMapping(value = "advert/new", method = RequestMethod.POST)
	public String postAdvert(@Valid Advert advert, BindingResult bindingResult, Model model) {

		if (bindingResult.hasErrors()) {
			return "Advertsview";
		}

		Advert advertCurrent = advertService.getAdvertById(advert.getId());
		if (advertCurrent != null) {
			advert.setType(AdvertType.ADVERT);
			advert.setCreation(new Date());
			advertService.editAdvert(advert);
		} else {
			advert.setType(AdvertType.ADVERT);
			advert.setCreation(new Date());

			advertService.createAdvert(advert);
		}

		model.addAttribute("myAdvert", advert);

		return "Advertshow";
	}

	@RequestMapping(value = "advert/show/{id}", method = RequestMethod.GET)
	public String showAdvert(@PathVariable long id, Model model) {

		List<Category> categories = advertService.getAllCategory();
		model.addAttribute("categories", categories);

		Advert advert = advertService.getAdvertById(id);
		model.addAttribute("myAdvert", advert);<commit_msg>Working with Principal<commit_after>package com.dta.services.controller;

import java.security.Principal;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dta.services.model.Advert;
import com.dta.services.model.AdvertType;
import com.dta.services.model.Category;
import com.dta.services.model.User;
import com.dta.services.service.IAdvertService;
import com.dta.services.service.IUserService;

@Controller
@RequestMapping("/")
public class AdvertController {
	
	@Autowired
	IAdvertService advertService;
	
	@Autowired
	IUserService userService;
	
	@ModelAttribute(value="user")
	public User user(){
		return new User();
	}
	
	@ModelAttribute(value="advert")
	public Advert advert(){
		return new Advert();
	}
	
	@RequestMapping
	public String home(Model model){
		
		return "Home";
	}
	
	@RequestMapping(value = "adverts")
	public String listAdverts(Model model) {

		return "Advertsview";
	}

	@Secured("hasAnyRole('USER', 'ADMIN')")
	@RequestMapping(value = "advert/new", method = RequestMethod.GET)
	public String postAdvert(Model model) {

		List<Category> categories = advertService.getAllCategory();
		model.addAttribute("categories", categories);
		
		model.addAttribute("advert", new Advert());

		return "PostAdvert";
	}

	@Secured("hasAnyRole('USER', 'ADMIN')")
	@RequestMapping(value = "advert/new", method = RequestMethod.POST)
	public String postAdvert(@Valid Advert advert, BindingResult bindingResult, Model model, Principal principal) {

		if (bindingResult.hasErrors()) {
			return "Advertsview";
		}
		
		principal.getName();
		

		Advert advertCurrent = advertService.getAdvertById(advert.getId());
		if (advertCurrent != null) {
			advert.setType(AdvertType.ADVERT);
			advert.setCreation(new Date());
			advertService.editAdvert(advert);
		} else {
			advert.setType(AdvertType.ADVERT);
			advert.setCreation(new Date());

			advertService.createAdvert(advert);
		}

		model.addAttribute("myAdvert", advert);

		return "Advertshow";
	}

	@RequestMapping(value = "advert/show/{id}", method = RequestMethod.GET)
	public String showAdvert(@PathVariable long id, Model model) {

		List<Category> categories = advertService.getAllCategory();
		model.addAttribute("categories", categories);

		Advert advert = advertService.getAdvertById(id);
		model.addAttribute("myAdvert", advert);
{'fraction_non_alphanumeric': 0.12447625698324022, 'fraction_numerical': 0.0, 'mean_word_length': 5.716295427901524, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2560269', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1718, 'n_words': 328}